0

Good day mate, i just want to ask how can i use a function from my controller inside my custom directive?

the scenario is a have a directive that's restrict the panel views for different user so i need to construct a function that's evaluate the user. i want to declare that function in a controller so i can use it to another controllers and the same time with directives too.

//my directive: 

app.directive("restricted", function() {
     return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            // Some auth check function
            var isAuthorized = checkAuthorization();
            if (!isAuthorized) {
                element.css('display', 'none');
            }
        }
    }
})

//my function inside controller
app.controller('myController', ['$scope', function() {

    $scope.checkAuthorization = function() {
           //Code here. . .
    }

})];

thanks guys

2
  • stackoverflow.com/questions/16839259/… You should search a bit more before asking question. Passing a function in parameters seems to be the more natural way. You can pass multiple function by passing a JSON containing multiples functions. Also consider declaring you functions in a factory or service if you want to use it in another controller. Commented Jun 9, 2017 at 3:41
  • Directives should only be calling parent controllers to communicate events. Visibility of a directive should be controlled by a Model value created by the Controller. In that case, use the ng-show directive. Commented Jun 9, 2017 at 4:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.