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
resolvestrue
- automatic calling of $destroy when
nfif
resolvefalse
) ?
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
Post a Comment