0

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

  1. 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);
         });
     });
    
  2. Service

    myapp.factory('UserSvc', function ($log, $q, $http) {
        return {
            frmOpened: false
        };
    });
    
  3. 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;
                });
        }
    };
    }]);
    

2 Answers 2

1

.on() is a jQuery method (also included in Angular's jqLite). The code inside the attached event handler lives outside of Angular, so you need to use $apply:

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of exception handling, executing watches.

For example:

element.on("click", function() {

  var parentElement = $(this).parent();

  scope.$apply(function() {
    if (parentElement.hasClass('sample')) {
      UserSvc.frmOpened = true;
    } else {
      UserSvc.frmOpened = false;
    }
  });

  return false;
});

Demo: http://plnkr.co/edit/mCl0jFwzdKW9UgwPYSQ9?p=preview

Also, the element in the link function is already a jqLite/jQuery-wrapped element, no need to perform angular.element() on it again.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, it works now but there is a lag (like 1-2 second but quite noticeable) between the time I click on the button with directive and the form showing based on changes to the controller showForm, so do you happen to know if this is due to using watch with ng-show to monitor changes to showForm or is it due to apply or the whole structure itself? Thanks
Sorry, really hard to say without seeing all the code that is involved.
0

It looks like you have some brackets where they shouldn't be in your directive. You don't have brackets surrounding the first part of your if statement, but you have a closing bracket before your else. I think this messes up things.

Try using this as your link-function and see if it changes anything:

link: function (scope, element, attrs) {
            angular.element(element).on("click", function () {
                var parentElement = $(this).parent();
                if (parentElement.hasClass('sample')) {
                    UserSvc.frmOpened = true; //Surrounded this with brackets
                } else {
                    UserSvc.frmOpened = false;
                }
                return false;
            });
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.