1

How to check if output binding is given?

Examplecode:

angular.module('tester', []);
angular.module('tester').component('test', {
    template: '<h3></h3>',
    bindings: {
      callback : '&'
    },
    controller: function() {
       // How to check if callback binding is given?
       // typeof this.callback === 'function' returns true
       // angular.isFunction(this.callback) also returns true 
    }
}); 
George Kagan
  • 5,913
  • 8
  • 46
  • 50
tiktak
  • 57
  • 10

2 Answers2

0

If you want to check if binding is given just do:

   if(this.callback)
      return true
    else 
      return false

The value of the this.callback will be available in the component if the binding is there otherwise it wont be.

Beslinda N.
  • 4,808
  • 5
  • 27
  • 33
0

If you change your binding to being optional, then it will be undefined if not supplied and you can check if it was given using the methods you've already mentioned.

bindings: {
  callback : '&?'
},