angularjs - persist $rootScope into directive -


i'm creating open/close profile view, user can click in anywhare close profile.

for now, created $rootscope variable ngif controlling.

// toggle functionality $rootscope.profileopened = false; $rootscope.userprofile = function () {     if ($rootscope.profileopened) {         $rootscope.profileopened = false;     } else {         $rootscope.profileopened = true;     } } 

and have directive in body bind click set $rootscope.profileopened false.

this.app.directive("globalevents", function ($rootscope) {     return {         link: function (scope, element, attrs) {             element.bind('click', function (e) {                 if ($rootscope.profileopened) {                     $rootscope.profileopened = false;                 }             });         }     } }); 

html directive

<body global-events> 

the $rootscope injection in directive working fine, when manipulate variable inside directive, original $rootscope.profileopened dos not change, sounds lose $rootscope reference.

what i'm missing?

the value change since event outside of angular need tell angular run digest using $apply() or $timeout() or won't see changes reflected in view.

try:

 element.bind('click', function (e) {       if ($rootscope.profileopened) {          $rootscope.profileopened = false;          scope.$apply();        }   }); 

if use ng-click angular automatically run digest internally.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -