angularjs – scope.$watch在指令链接函数中没有被调用

我有这个工厂,
.factory('authentication',[function() {

    return {
      loginrequired: false
    };
  }]);

我有这个控制器,

.controller('TopNavCtrl',['$scope','authentication',function($scope,authentication) {

  $scope.login = function() {
    authentication.loginrequired = true;
  };

}]);

我在指令中有这个链接功能,

link: function(scope,element,attrs) {

    scope.show = false;

    scope.$watch(authentication.loginrequired,function(value) {
      scope.show = value;
    });
  }

当authentication.loginrequired = true时;是在控制器中完成的,范围.$watch中的指令未被调用.

有什么想法吗?

Scope.$watch接受作为第一个参数表达式或函数.您作为第一个参数传递的是存储在authentict.loginrequired中的值.

以下工作(假设您已正确注入认证工厂):

link: function(scope,attrs) {

    scope.show = false;

    scope.$watch(function(){return authentication.loginrequired;},function(value) {
      scope.show = value;
    });
  }

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...