I have the following directive, service and controller inside my AngularJS app. The service is common between the directive and this controller (as well as other controllers using the same service in my app). As shown inside the controller I watch the service for changes, while in directive I communicate with the service to update it. For some reason which I don't know the directive is not updated my service, so can someone please tell me what I am doing wrong here? Thanks
Controller:
myapp.controller('ClientsCtrl', function ($scope, UserSvc) { $scope.showForm = UserSvc.frmOpened; $scope.$watch(function () { return UserSvc.frmOpened; }, function () { $scope.showForm = UserSvc.frmOpened; console.log('Changed... ' + $scope.showForm); }); });Service
myapp.factory('UserSvc', function ($log, $q, $http) { return { frmOpened: false }; });Directive:
myapp.directive('myDirective', ['UserSvc', function (UserSvc) { return { restrict: 'A', link: function (scope, element, attrs) { angular.element(element).on("click", function () { var parentElement = $(this).parent(); if (parentElement.hasClass('sample')) UserSvc.frmOpened = true; //This code never update the service } else { UserSvc.frmOpened = false; //This code never update the service } return false; }); } }; }]);