I am trying to place a watch on controller variable which gets updated from a directive using function mapping. variable is getting updated and logged in console but watch on it not working.
Code Snippet :
index.html
<body ng-app="myApp" ng-controller="myCtrl"> <div> <test on-click="update()"></test> </div>
app.js
var myApp = angular.module('myApp', []); myApp.controller('myCtrl', function($scope){ $scope.test = { value: false }; $scope.update = function() { $scope.test.value = !$scope.test.value; console.log("Update: " + $scope.test.value); }; $scope.$watch('test', function(newVal){ console.log("Watch: " + newVal.value); }, true); }); myApp.directive('test', function($compile){ return { restrict: 'E', transclude: true, replace: true, scope: { onClick: '&' }, template: '<div ng-transclude=""></div>', link: function(scope, element, attrs) { var $buttonElem = $('<button>Test</button>').appendTo(element); $buttonElem.click(function(){ scope.onClick(); }); } } });
Plunker Link is : https://plnkr.co/edit/41WVLTNCE8GdoCdHHuFO?p=preview
scope.$apply()
in the link function.$timeout(function() { $scope.$apply(); });
inside your controller update function which would do the same thing (even if it is ugly)