Is there a built-in directive, like ng-click
, that emits an event instead of calling a function? I want to do something like this:
HTML:
<button ng-click-emit="foo" ng-click-emit-model="model.bar">Emit foo</button>
Child controller:
$scope.model = {bar: 'baz'};
Parent controller:
$scope.$on('foo', function(event, arg) {
console.log(arg); // prints 'baz'
});
If you just need to trigger a click event, you can omit the line that begins with for( . @Parag: Read it again. The loop is to click the same link 50 times, which is what it does.
The onclick event occurs when the user clicks on an element.
Event Handlers and Event Listeners. When a user clicks a button or presses a key, an event is fired. These are called a click event or a keypress event, respectively.
I figured it out: just call the scope's $emit
function directly from ng-click
. So the HTML in my example would become:
<button ng-click="$emit('foo', model.bar)">Emit foo</button>
The same should work for $broadcast
too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With