所有
就像标题所说,我把ng-controller和指令放在<body>
标签上,如:
<body ng-controller="main" global-control>
控制器和指令如:
.controller("main", function($scope){
$scope.name = "hello";
})
.directive("globalControl", function($timeout){
return {
restrict: "A",
scope:false,
link: function(scope, EL, attrs){
EL.on("click", function(){
scope.name = "hola"
});
}
}
})
我已在指令中指定没有新的范围创建,但为什么当我点击正文时,名称无法更改为&#34; hola&#34;?只有改变它的方法是调用范围。$ apply wrap it?
答案 0 :(得分:0)
对于由JQLite(或JQuery)绑定的事件处理程序,需要通知AngularJS框架对范围的更改。请使用scope.$apply
:
.controller("main", function($scope){
$scope.name = "hello";
})
.directive("globalControl", function($timeout){
return {
restrict: "A",
scope:false,
link: function(scope, EL, attrs){
EL.on("click", function(){
//scope.name = "hola"
scope.$apply("name='hola'");
});
}
}
})
有关详细信息,请参阅AngularJS $rootScope.scope API Reference -- $apply