$ emit event when clicking on an element

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:

 $scope.model = {bar: 'baz'}; 

Parent controller:

 $scope.$on('foo', function(event, arg) { console.log(arg); // prints 'baz' }); 
+7
angularjs
source share
1 answer

I realized: just call the scope $emit directly from ng-click . Thus, the HTML in my example will be as follows:

 <button ng-click="$emit('foo', model.bar)">Emit foo</button> 

The same should work for $broadcast .

+12
source share

All Articles