Angular 1.5 Component Bindings: Check if Callback is Present Angular 1.5 Component Bindings: Check if Callback is Present angularjs angularjs

Angular 1.5 Component Bindings: Check if Callback is Present


The simplest way would to check if the attribute is defined. In your controller inject $attrs and then you can do:

if( $attrs.onRemove ) { //Do something }

Using the & binding angular will wrap the function in order to keep references to the original $scope of the passed method, even if is not defined.


Execute the function onRemove on component allow to get if a function was passed in parameter. So you can use ng-if="$ctrl.onRemove()"

component('contactList', {template:   `<div ng-repeat="c in $ctrl.contacts">     {{c.name}}     <div ng-click="$ctrl.onRemove()({contact: c})" ng-if="$ctrl.onRemove()">Remove</div>   </div>`,bindings: {  contacts: '<',  onRemove: '&'},controller: function() {  console.log(this.onRemove);  console.log(this.onRemove());}})