2016-01-12 114 views
0

我想編寫一個指令,顯示/隱藏基於我提供的角色作爲參數(字符串值)的元素。angularjs:如何訪問指令(文本)值

我有以下指令:

(function() { 
    'use strict'; 

    angular 
     .module('fuse') 
     .directive('showWhenRole', showWhenRoleDirective); 

    /** @ngInject */ 
    function showWhenRoleDirective(auth) { 
     return { 
      restrict: 'A', 
      scope: { 
       showWhenRole: '@' 
      }, 
      compile: function(scope, tElement) { 
       console.log("showWhenRoleDirective",scope.showWhenRole); 
       // if (auth.isAdmin()) { 
       //  tElement.show(); 
       // } else { 
       //  tElement.hide(); 
       // } 
      } 
     }; 
    } 
})(); 

我的HTML元素如下所示:

<md-menu-bar id="user-menu" show-when-role="admin"> 

當我看在控制檯,該消息是:

showWhenRoleDirective undefined 

什麼我做錯了嗎?

+0

是可用的價值在你的範圍'admin'一個布爾值,或者是你需要覈對該字符串值。我假設它是一個字符串值,因爲你已經用'@'賦值定義了隔離範圍。 – jusopi

+0

好問題。它是一個字符串值,因此我在範圍部分 –

+0

中使用了'@',爲什麼不使用'ng-show'或'ng-hide'? – jusopi

回答

1

移動從compilelinkscopecompile階段