亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AngularJs中ng-controller下的函數在調用時為什么會執行兩次 ?

AngularJs中ng-controller下的函數在調用時為什么會執行兩次 ?

守著一只汪 2018-11-14 15:11:02
AngularJs中ng-controller下的函數在調用時為什么會執行兩次 
查看完整描述

1 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="angular.min.js"></script>
<script>
var myApp = angular.module("myApp", []);
myApp.directive('testIt',function(){
return {
restrict: 'A',
scope: false,
link:function(scope, elm, attr){
if(scope.$last){
elm.css('color','red');
scope.myFunction();
}
}
}
});
myApp.controller("testCtrl", function($scope){
$scope.items = [0,1,2,3,4];
$scope.myFunction = function(){
console.log('Hello!');
};
});
</script>
</head>

<body>
<div ng-app="myApp">
<div ng-controller="testCtrl">
<ul>
<li ng-repeat="item in items" test-it >{{item}}</li>
</ul>
</div>
</div>
</body>
</html>

主要在于12行的scope:false,這個是默認的,其實你不寫也是false。這樣drective繼承了父scope,所以可以調用父作用域的方法,而聲明新的scope即scope:{}形式就不會繼承了,不過你依然可以用scope.$parent.myFunction()的方式調用。

查看完整回答
反對 回復 2018-12-08
  • 1 回答
  • 0 關注
  • 1014 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號