Ale*_*Man 2 watch angularjs angularjs-directive angularjs-watch
我已经创建了一个应用程序是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);
}
};
});
Run Code Online (Sandbox Code Playgroud)
小智 5
我纠正了它.工作小提琴
<div ng-app="myapp">
<div ng-controller="TreeCtrl">
<input type="text" ng-model="userName"/>
<button ng-click="changeValue()">Change</button>
<tree name="name">
</tree>
</div>
</div>
module.directive("tree", function($compile) {
return {
restrict: "E",
transclude: true,
scope: {
name: '='
},
template:'<div>sample</div>',
link : function(scope, elm, $attrs) {
function update()
{
};
scope.$watch('name', function(newVal, oldVal) {
console.log('calling');
update();
}, true);
}
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11774 次 |
| 最近记录: |