javascript - AngularJS : Directive transcluded scope lost -
I am creating a directive, I say "need to call" - authorization? ??? NG-I want to use it in this way to wrap the directive:
& lt; Need - Authority role = 'SuperUusser' & gt; & Lt ;! Â ???? Super Right user stuff goes here, within the scope of the controller of this scene; Line & Gt; & Lt; / Requirement authority & gt; I've got it as:
angular.module ('myApp'). Instructions ('require authorization', function () {Rest: {lt; div ng-if = \ 'iAmInRole' 'ng-transclude & gt; & lt; / div & gt;', restrict: 'e' , Passed: true, scope: {role: '@'}, controller: function ($ scope, user service) {$ scope.iAmInRole = (UsersService.myRoles.indexOf ($ scope.role)! == -1); }};}); This works, but loses the scope of the contents contained in the directorate, especially in the scope of the controller to see it. What i see
jsfiddle for reference: Notice how the auth value is not displayed inside the instruction, but is available outside of the instructions.
both ng-if and ng-transcribe < / code> Instructions are incorporated into your instructions. In this case build-in transcription mechanism is not working properly and you have to implement your own ngIf to do the desired work: Javascript app.directive ('requireAuthorization', function () {return {template: '& lt; div ng-transclude & gt; & lt; / div & gt;' : 'E', passed: true, scope: {role: '@'}, controller: function ($ scope) {$ scope.iAmInRole = true;}, link: function (scope, element, attri, CTN, transclefafen) {Trans Clufon (function (clone) {// & lt; = override default transact element. Meta (); if (scope.IAmInRole) {// Plunker:
If ng-show is an option for you instead of using ng-if This can be a very simple solution, the only side effect is that the hidden data will be presented in the DOM and CSS .ng-hide {display: none! Important;} . JSFiddle:
This post may also be useful to you because it describes a similar problem:
Comments
Post a Comment