0

I have a component with an optional '&' binding.

How can I check whether a method was passed into the component through this binding?

assuming that:

bindings: {
    onChange: '&?',
}

When I run if (this.onChange) it always evaluates to an angular wrapper (and thus true):

ƒ (locals) {
    return parentGet(scope, locals);
}

I read this post and tried by checking if (this.$attrs.onChange), but that doesn't work either. $attrs.onChange evaluates to:

if (this.$attrs.onChange) {
    console.log(this.$attrs.onChange) // $resolve.onChange()
    console.log(typeof this.$attrs.onChange); //string
}

What am I doing wrong? Running on Angular 1.5.8

georgeawg
  • 48,608
  • 13
  • 72
  • 95
user2954463
  • 2,362
  • 2
  • 23
  • 37
  • Are you checking `this.onChange` after `$onInit` is called? – Jake Holzinger Mar 26 '18 at 22:19
  • What are you trying to accomplish? If the `on-change` attribute exists, expression `&` binding will always bind a function to the controller `this` context. When invoked, the bound function will evaluate the attribute as an [AngularJS expression](https://docs.angularjs.org/guide/expression) using the parent scope of the component. – georgeawg Mar 27 '18 at 00:28
  • 1
    Possible duplicate of [How to check if a method argument of a directive is specified in AngularJS?](https://stackoverflow.com/questions/21935099/how-to-check-if-a-method-argument-of-a-directive-is-specified-in-angularjs) – georgeawg Mar 27 '18 at 00:59
  • @JakeHolzinger yes, I'm checking `this.onChange` after `$onInit` – user2954463 Mar 27 '18 at 20:07
  • @georgeawg i linked to that question in my question. it doesn't seem to answer the question for angular components (just directives and controllers) – user2954463 Mar 27 '18 at 20:08
  • Under-the-hood, components are converted to a Directives Defiinition Object and compiled by the [$compile service](https://docs.angularjs.org/api/ng/service/$compile). The behavior of expression `&` binding is the same. What are you trying to accomplish with this component that has not been answered? – georgeawg Mar 27 '18 at 22:14

1 Answers1

0

You can probably check the type of the passed onChnage to be a function like: typeof this.onChange === 'function'

arbghl
  • 358
  • 2
  • 14