I think the angular-recommended way of doing callbacks (like ngclick for example) is to use the method binding type: &
Here is a plunkr example: https://plnkr.co/edit/nySL4OoMpJPkGXX8j78U?p=preview
Note this part, the method binding type is defines, and the internalChange function executes whatever angular expression is provided by the parent/caller.
bindings: {
valueChanged: '&?'
},
controller: function() {
this.$onInit = function() {
this.value = 'initial value';
};
this.internalChange = function() {
if (this.valueChanged) {
this.valueChanged({ $value: this.value} );
}
};
}
And then here are 2 ways of many to use the expressions, note $value as defined by the component.
<my-comp data-value-changed="$ctrl.someFunction($value)"></my-comp> <br
<my-comp data-value-changed="$ctrl.someProperty = 'Bla: ' + $value"></my-comp>
I tried to set it up to show a variety of things.
The method binding takes an angular expression. I have 2 examples, one calling a function on the parent controller, the other setting a property
This expression is executed with the scope of the parent/caller (ie: has access to parent controller properties, functions etc...)
The component is the one who invokes it by executing the binding name as a function
The component can provide a map of key/value, where the key is a named property that can be used by the caller ($value in my example)
This is a good solution so long as you are OK with the component doing "push" updates to the parent. If for example you only wanted to fetch the data from the component at a specific time (possibly due to time or memory constraints), than there are many other different solutions for that problem.
Hope this helps!