4

I have a directive with the following scope:

scope: {
  copyObject: '&',
  deleteObject: '&'
}

Now, I have a corresponding button in my template which looks like this:

 <button ng-click="copyObject()">Copy</button>

However, I would like to hide the button if there's no function applied to copy-object, for example:

<!-- Should show the copy button -->
<my-directive copy-object="copyObject()"></my-directive>

<!-- Should not show the copy button -->
<my-directive></my-directive>

So I applied the following to my template:

<button ng-click="copyObject()" ng-if="copyObject">Copy</button>

But this does not seem to work, if I check the directives real isolated scope, I notice that, even when the attribute is not entered, the function still exists, so the button is always visible.

Is it possible to detect if a function is bound to the copyObject() or not? And is it a good practice to do so? I'm not sure if the directive should be aware of the bound function or is this some kind of scope access violation?

An example: http://jsfiddle.net/azchpo5q/ (the second button should not be visible because there is no action bound to it).

3
  • The clicking is not the problem (that already works with brackets), but I would like to hide the button if no function is bound to it. I added a fiddle: jsfiddle.net/azchpo5q The second button should not be visible as it has no copy-object attribute. Commented Oct 29, 2014 at 10:53
  • 1
    I found the answer: stackoverflow.com/questions/21935099/… (I also voted to close as a duplicate) Commented Oct 29, 2014 at 11:03
  • Thx g00glen00b, your answer solved my problem! Commented Nov 8, 2016 at 11:25

1 Answer 1

3

You can use a function in ng-if to determine if the function is defined.

<button ng-click="copyObject()" ng-if="doShowCopy()">Copy</button>

Then define this method:

 $scope.doShowCopy = function() {
     return (typeof $scope.copyObject == 'function');
 }
Sign up to request clarification or add additional context in comments.

7 Comments

The problem is that typeof $scope.copyObject is always a function, because this is a wrapper function not direct function that comes from outer scope.
Sure? I am guessing it might be defined in the scope, but not as a function!!
It is defined as a function yes, if I use directive.isolateScope().copyObject it returns a function. I solved it thanks to another question though, by using the attrs from the link() function. I marked it as a duplicate.
Absolutely sure. This function which basically tries to evaluate a function in parent scope.
@Prasad It assumes that because the function is bound as &. So AngularJS knows that it should be a function and it returns a wrapper, like @dfsq mentioned. You can see it in this fiddle: jsfiddle.net/azchpo5q/2
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.