I can't figure this one on my own. Maybe I'm missing something. I have a controller and a directive which creates its own scope.
Plunker link : http://run.plnkr.co/plunks/wFV7d2blZKEXUgHIOxYo/
Here is the controller code which just creates 3 variables and exposes a "change" function for each one :
var myApp = angular.module("myApp",[]);
myApp.controller('MainController', function ( $scope, $rootScope ) {
$rootScope.showStuff = true;
$scope.showStuff2 = true;
$scope.showStuffObj = { stuff : true };
$scope.changeStuff = function () {
$rootScope.showStuff = false;
}
$scope.changeStuff2 = function () {
$scope.showStuff2 = false;
}
$scope.changeStuff3 = function () {
$scope.showStuffObj.stuff = false;
}
});
Coming up next, the directive:
myApp.directive("mydirective", function () {
return {
scope : {
showStuff : "=",
showStuff2 : "=",
showStuffObj : '='
},
restrict : "EA",
template : "<h2>Running</h2>",
link : function ( $scope ) {
console.log("running directive", $scope);
$scope.$watch("showStuff", function () {
console.log($scope.showStuff);
});
$scope.$watch("showStuff2", function () {
console.log($scope.showStuff2);
});
$scope.$watch("showStuffObj", function () {
console.log($scope.showStuffObj);
});
}
};
});
Why do I get this ?
If the two way binding works, I should see the real values of the variables, not undefined. Why doesn't the watch update when I update the variables? This is very confusing.