I've got a directive that shares data with its controller, which I'm doing with an isolate scope.
However, when I update the data from inside the directive, neither the directive nor the controller sees the data change using $watch.
Here's an example of what I'm trying to do:
var app = angular.module('myApp', []);
app.controller('myAppCtrl', function($scope) {
$scope.buttonSettings = {
val: 0
}
$scope.$watch('buttonSettings.val', function(val) {
console.log('watchInController')
console.log(val)
});
});
app.directive('buttonTest', function() {
return {
restrict: 'E',
scope: {
buttonSettings: '='
},
template: '<button>{{buttonSettings.val}}</button>',
link: function(scope) {
var button = $('button');
console.log(scope.buttonSettings)
scope.$watch('buttonSettings.val', function(val) {
console.log('watchInDirective')
console.log(val)
});
button.bind('mouseover', function() {
scope.buttonSettings.val++;
console.log('mouseover')
console.log(scope.buttonSettings.val)
})
}
}
});
And here's the full example in plunkr: http://plnkr.co/edit/lnztoMhtU03Qzl0BaMpk?p=preview
What am I doing wrong?