piernik
piernik

Reputation: 3657

Check if expression in directive is defined?

In my directive I use expression:

return {
    restrict: 'E',
    scope: {
        func: "&?"
    },
    controller: 'directiveCtrl',
    controllerAs: 'vm',
    bindToController: true
}

function directiveCtrl() {
    var vm = this;
    vm.isFunc = angular.isDefined(vm.func) || false;
}

How to check if func is defined? Now it's always defined:/

<my-directive></my-directive>

<my-directive func='myFunc()'></my-directive>

Upvotes: 0

Views: 66

Answers (2)

Sander_P
Sander_P

Reputation: 1835

I think this should work:

function directiveCtrl($scope) {
    var vm = this;
    vm.isFunc = angular.isDefined($scope.func) || false;
}

Upvotes: 0

bhanu.cs
bhanu.cs

Reputation: 1365

You can do it in the link function of the directive as below:

 return{
     restrict: 'E',
        scope: {
            func: "&?"
        },
        controller: 'directiveCtrl',
        controllerAs: 'vm',
        bindToController: true,
        link:function(scope,element,attrs){
         if(attrs.func) {
         attrs.$observe('func', function(value) {
         console.log(value);
       });
      }
    }

Upvotes: 2

Related Questions