的JavaScript:
var App = angular.module('App', []);
App.controller('RootCntr', function ($scope) {
$scope.openedShelf = false;
console.log('controller', $scope.openedShelf);
setTimeout(function() {
$scope.openedShelf = true;
console.log('controller', $scope.openedShelf);
}, 2000);
});
App.directive('shelf', function () {
return {
restrict: 'E',
scope: {
'open': '='
},
link: function (scope, element, attrs) {
console.log('linked');
scope.$watch('open', function (newVal, oldVal) {
console.log(newVal);
if (newVal) {
console.log(true, newVal);
}
}, true);
}
};
});
的HTML:
<body ng-app="App">
<div ng-controller="RootCntr">
<shelf open='openedShelf'></shelf>
</div>
</body>
当我更改指令中的值时openedShelf
,RootCntr
它的 watch 语句没有捕捉到更新。有任何想法吗?