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
Post a Comment