angularjs:如何访问指令(文本)值

angularjs: howto access directive (text) value

本文关键字:指令 文本 访问 何访问 angularjs      更新时间:2023-09-26

我想编写一个指令,根据我作为参数(字符串值)提供的角色来显示/隐藏元素。

我有以下指令:

(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

我做错了什么?

compile移动到linkscopecompile阶段中不可用