Angularjs directive conditional rendering without ng-if -


i have directive render if authentication service tells me so:

<div my-loggedin-directive ng-if="security.isauthenticated()"></div> 

the directive quite empty :

.directive('myloggedindirective', [     function() {         return {             templateurl: 'modules/myloggedindirective.tpl.html',             restrict: 'a',             replace: true,             link: function($scope) {                  $scope.$on('$destroy', function() {                      console.log('$destroy');                  });             }         };     } ]); 

since directive should rendered when i'm logged in, ngif logic should inside directive declaration, , not in ng-if (the directive without ngif totally broken).

how can change directive code (injecting test on security service inside directive declaration) knowing want same behavior ngif ? :

  • directive present in dom when nfif resolves true
  • automatic calling of $destroy when nfif resolve false) ?

i tried use compile function , watcher watch security.isauthenticated() without success

declare directive attribute pass security object directive:

<div my-loggedin-directive my-security='security'></div> 

and directive should include scope, make sure uses two-way binding:

.directive('myloggedindirective', [ function() {     return {         scope: {             security: "=mysecurity"         }         templateurl: 'modules/myloggedindirective.tpl.html',         restrict: 'a',         replace: true,         link: function($scope) {              $scope.$on('$destroy', function() {                  console.log('$destroy');              });               $scope.$watch('security', function(newval, oldval) {                  if (newval.isauthenticated()) {                  //                  } else {                  //                  }              });         }     }; } 

]);

and can access $scope.security variable in temple myloggedindirective.tpl.html

<div ng-if="security.isauthenticated()">...</div> 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -