Here is the my code
var directive = {
restrict: 'E',
templateUrl: '/static/myTemplate.html',
scope: {
},
controller: ["$scope", controllerFunc],
controllerAs: 'multiCheckboxCtrl',
bindToController: true,
link: linkFunc,
};
return directive;
function controllerFunc($scope) {
// $watch List
/**
* Event handler for searchText change
*/
$scope.$watch(angular.bind(this, function () {
return $scope.multiCheckboxCtrl.clickedElsewhere.value;
}), function (newVal) {
if (newVal !== undefined && newVal !== "") {
console.log(newVal);
console.log("I am here");
}
});
}
function linkFunc(scope, element, attr){
// Detect Element Click Logic
scope.multiCheckboxCtrl.clickedElsewhere = {value: false};
$document.on('click', function(){
scope.multiCheckboxCtrl.clickedElsewhere.value = false;
console.log(scope.multiCheckboxCtrl.clickedElsewhere);
});
element.on('click', function(){
event.stopPropagation();
scope.multiCheckboxCtrl.clickedElsewhere.value = true;
console.log(scope.multiCheckboxCtrl.clickedElsewhere);
});
// End Detect Element Click Logic
}
The linkFunc basically determines if the directive element is clicked or not.
I verified that whenever the directive element is clicked it consoles true, and when any element outside directive element is clicked it consoles false.
However it seems like my $watch in the controller is not catching the change.
Can anyone show me what is going wrong
Thanks
scope.$apply(). Like this$document.on('click', function(){ scope.multiCheckboxCtrl.clickedElsewhere.value = false; console.log(scope.multiCheckboxCtrl.clickedElsewhere); scope.$apply() })