【发布时间】:2014-03-17 18:22:15
【问题描述】:
myApp.directive('myView', ['myService',function($scope) {
return {
restrict : 'C',
template : '',
templateUrl : 'myView.html',
controller : function($scope,myService){
$scope.items = myService.$data;
$scope.$watch('myService.$data' , function(newVal, oldVal, scope) {
console.log("Watching items ...");
});
},
};
}]);
我正在使用上面的代码,我想在其中观看 myService.$data ,它是在其服务中定义的。 $watch 在控制器加载时第一次被调用,在代码的其他地方我更新了服务变量 $data。当我使用 ng-repeat 指令并与 $data 绑定时,这会反映在 UI 中。
根据我在 UI 更新之前的理解,必须再次调用这个特定的手表,但事实并非如此。所以我假设我没有正确使用手表。
【问题讨论】:
标签: angularjs angularjs-scope angularjs-ng-repeat