Wednesday, 7 August 2013

Directive's $watch not watching variable in scope

Directive's $watch not watching variable in scope

The javascript:
var App = angular.module('App', []);
App.controller('RootCntr', function ($scope) {
$scope.openedShelf = 'ho';
console.log('controller', $scope.openedShelf);
setTimeout(function() {
$scope.openedShelf = 'hey';
console.log('controller', $scope.openedShelf);
}, 2000);
});
App.directive('shelf', function () {
return {
restrict: 'E',
scope: false,
link: function (scope, element, attrs) {
console.log('linked');
scope.$watch('hip', function (newVal, oldVal) {
console.log(newVal);
if (newVal) {
console.log(true, newVal);
}
}, true);
}
};
});
The html
<body ng-app="App">
<div ng-controller="RootCntr">
<shelf open='openedShelf'></shelf>
</div>
</body>
When I change the value of openedShelf in RootCntr the directive isn't
catching the update with it's watch statement. Any ideas?

No comments:

Post a Comment