0

I want to write a directive that shows/hides the element based on the role I'm providing as a parameter (string value).

I have the following directive:

(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();
                // }
            }
        };
    }
})();

My HTML element looks as follows:

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

When I look in the console, the message is:

showWhenRoleDirective undefined

What am I doing wrong?

11
  • is the value of admin a boolean value on your scope or is this a string value you need to check against. I'm assuming it's a string value because you've defined the isolate scope with the @ assignment. Commented Jan 12, 2016 at 18:25
  • Good question. Its a string value, hence I used the '@' in the scope section Commented Jan 12, 2016 at 18:26
  • also, why aren't you using ng-show or ng-hide? Commented Jan 12, 2016 at 18:26
  • 3
    move from compile to link, scope is not available in compile phase. also check scope/$scope Commented Jan 12, 2016 at 18:28
  • 1
    ahhhh good catch @entre. Yeah you need this to be inside the link function, the $scope is not available in the compile phase. Commented Jan 12, 2016 at 18:30

1 Answer 1

1

move from compile to link, scope is not available in compile phase

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.