【发布时间】:2015-01-31 15:50:52
【问题描述】:
我创建了一个应用程序是 angularjs,其中我有一个指令,当 $rootScope 变量发生变化时,我在指令中进行监视以触发指令中的某些方法,但问题是当 $ rootScope.name 值已更改,指令中的手表不起作用
我的代码如下所示
var module = angular.module('myapp', []);
module.controller("TreeCtrl", function($scope, $rootScope) {
$scope.treeFamily = {
name : "Parent"
};
$scope.changeValue = function()
{
$rootScope.name = $scope.userName;
};
});
module.directive("tree", function($compile) {
return {
restrict: "E",
transclude: true,
scope: {},
template:'<div>sample</div>',
link : function(scope, elm, $attrs) {
function update()
{
};
scope.$watch('name', function(newVal, oldVal) {
console.log('calling');
update();
}, true);
}
};
});
【问题讨论】:
标签: angularjs angularjs-directive watch angularjs-watch