在角度自定义指令中未定义的值

问题描述:

如果用户没有查看权限,我试图制作销毁DOM元素的指令。我这样做如下:在角度自定义指令中未定义的值

angular 
     .module('digital_equation.auth') 
     .controller('AuthLoginController', AuthLoginController) 
     .directive('ngPermission', ngPermissionDirective); 

function ngPermissionDirective() { 
    return { 
     multiElement: true, 
     scope: { 
      ngPermission: '@' 
     }, 
     restrict: 'A', 
     link: function (scope, element, attributes) { 
      scope.$watch('ngPermission', function (value) { 
       console.log(value); 
       //Access rootScope to get the current user permissions 
       var permissions = scope.$root.globals.currentUser.role.settings.permissions; 
       //Loop through permissions object to check if any permission is the same as the value sent to directive 
       var keepGoing = true; 
       angular.forEach(permissions, function (permission, key) { 
        if (keepGoing == true) 
        { 
         if (key == value) { 
          element.show(); 
          keepGoing = false; 
         } 
         else { 
          element.hide(); 
         } 
        } 
       }); 
      }) 
     } 
    }; 
} 

HTML:

<div class="col-xs-12 col-md-9" ng-permission="manage_appointments"></div> 

在这种情况下,例如,如果从rootScope采取的当前用户没有权限“manage_appointments”中它是这个div应该是权限销毁。正如你所看到的,我只知道如何隐藏它,但这是不够的,我需要它在页面上被破坏,所以检查这个div没有显示出来。 我的第二个问题是console.log(value);返回undefined不管我尝试过什么。 我还需要访问rootScope的建议。如果我在这里通过rootScope作为参数,它可以工作,但我无法通过范围。所以我怎么做到这一点。

link: function(rootScope, element, attributes) 

请记住,即使我宣布我的指令在authController我需要它在我的整个项目中可用。 抱歉的描述,但它是我在AngularJs中的第一个自定义指令,我尝试了很多选项。 谢谢大家的时间和帮助!

编辑:

scope.$watch('ngPermission', function (value) 

这解决了我的价值的问题被不确定的,但是当我尝试使用两种不同元素的指令(一个被隐藏,一个用于显示),它会做什么,最后使用我的指令(在这种情况下显示)。任何想法为什么发生这种情况

解决方案:

function ngPermissionDirective() { 
    return { 
     multiElement: true, 
     priority: 900, 
     scope: { 
      ngPermission: '@' 
     }, 
     restrict: 'A', 
     link: function (scope, element, attributes) { 
      scope.$watch('ngPermission', function (value) { 
       console.log(value); 
       //Access rootScope to get the current user permissions 
       var permissions = scope.$root.globals.currentUser.role.settings.permissions; 
       //Loop through permissions object to check if any permission is the same as the value sent to directive 
       var keepGoing = true; 
       angular.forEach(permissions, function (permission, key) { 
        if (keepGoing == true) 
        { 
         if (key == value) { 
          element.show(); 
          keepGoing = false; 
         } 
         else { 
          element.remove(); 
         } 
        } 
       }); 
      }) 
     } 
    }; 
} 
+0

'@''尝试的ngPermission也许bacause: '='' – Leguest

+0

首先,你正在看attribute.yourValues,你应该看的范围。所以:'scope.ngPermission'。看看文档和示例,了解如何绑定和观看作品。 而关于删除DOM元素,为什么不使用'ng-if'或者用你的指令包装呢?我认为可能会更好,而不是手动与DOM战斗。 希望它有帮助。 – Ruben

尝试改变scope.$watch(attributes.ngPermission, ...scope.$watch('ngPermission', ...

另一种方法可能是$observe - attributes.$observe('ngPermission', ...

+0

这解决了我的问题,值是未定义的,但element.hide()仍然不起作用。我不知道为什么,因为它在发送正确权限时访问show元素,但不管我使用我的指令的div是什么消失。 – Alphonse

+0

修正了它,非常感谢您的帮助。 – Alphonse

+1

对不起,我之前没有回复您的评论 - 我正在打电话。我很高兴你让它工作! –