1

I know you can have optional property using a question mark like x:"=?" and then you can check if it is specified by checking if x is undefined or null.

How can I do similar thing for a function? Oftentimes, I want to hide a control if the function is not specified. I have to define another property for this purpose to workaround this problem. I wonder if there is a way to save this extra property.

newman
  • 6,841
  • 21
  • 79
  • 126
  • Possible duplicate of [How to check if a method argument of a directive is specified in AngularJS?](http://stackoverflow.com/questions/21935099/how-to-check-if-a-method-argument-of-a-directive-is-specified-in-angularjs) – fracz Jun 14 '16 at 16:30

1 Answers1

2

I found an answer to this question at here. Here is a recap in case somebody is interested:

In directive:

scope: {
    callback: '&'
},
link: function(scope, elem, attrs) {    
  scope.hasCallback = function() {
    return angular.isDefined(attrs.callback);
  }
}

In html:

<a href="" ng-show="hasCallback()" ng-click="callback()">Call me back</a>

I like it very much because it saves me an extra parameter.

Community
  • 1
  • 1
newman
  • 6,841
  • 21
  • 79
  • 126