范围。$手表指令链接功能没有得到所谓的 [英] scope.$watch in directive link function not getting called
本文介绍了范围。$手表指令链接功能没有得到所谓的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的工厂,
.factory('authentication', [function() {
return {
loginRequired: false
};
}]);
我有这样的控制器,
I have this controller,
.controller('TopNavCtrl', ['$scope', 'authentication', function($scope, authentication) {
$scope.login = function() {
authentication.loginRequired = true;
};
}]);
和我有一个指令,这个链接功能,
and I have this link function in a directive,
link: function(scope, element, attrs) {
scope.show = false;
scope.$watch(authentication.loginRequired, function(value) {
scope.show = value;
});
}
在 authentication.loginRequired = TRUE;
在控制器完成后, $范围看
中的指令不叫。
When authentication.loginRequired = true;
is done in the controller, the scope.$watch
in the directive isn't called.
任何想法,为什么?
推荐答案
范围。$看接受作为第一个参数前pression或功能。你所传递的第一个参数是一个值保存在 authetication.loginRequired
。
Scope.$watch accepts as first parameter expression or function. What you are passing as first parameter is a value stored in authetication.loginRequired
.
下面的工作(假设你已经正确地注入 authetication
厂):
Following will work (assuming you have correctly injected authetication
factory):
link: function(scope, element, attrs) {
scope.show = false;
scope.$watch(function(){return authentication.loginRequired;}, function(value) {
scope.show = value;
});
}
这篇关于范围。$手表指令链接功能没有得到所谓的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文