How to access angular component events in Parent controller Angular JS,
I have an angular component with button click event
is it possible to access/implement these events in Parent controllers?
How to access angular component events in Parent controller Angular JS,
I have an angular component with button click event
is it possible to access/implement these events in Parent controllers?
Instead of accessing child component events, your better option is to pass event using bindings. The true angular way would probably be using "outputs" of the component with callback functions, that child can invoke when necessary. In this case you would kind of use intermediate event mechanism. Implementation-wise it could look like this:
angular.module('demo', [])
.component('parent', {
template: `
<div>
<child on-select="$ctrl.itemSelected(item)"></child>
</div>
`,
controller() {
this.itemSelected = function(item) {
alert(item.name)
}
}
})
.component('child', {
bindings: {
onSelect: '&'
},
template: `
<button ng-click="$ctrl.onSelect({item: {name: 'Thomas'}})">Select</button>
`
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
<div ng-app="demo">
<parent></parent>
</div>