Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs ng-click VS jquery on('click')

I'm new to angular and failing to understand why should i use angular ng-click over jQuery event listeners? Since the best practice is to use only one of them in my App (according to this highly up voted answer) Isn't there any elegant solution in angular to listen to events such as in jquery?

jQuery

<span>Box 1</span>
<span>Box 2</span>
<span>Box 3</span>

$("span").on('click',function() {
  // do something 
});

Angular

<span ng-click="doSomething();">Box 1</span>
<span ng-click="doSomething();">Box 2</span>
<span ng-click="doSomething();">Box 3</span>

$scope.doSomething = function(){
    // do something
};

What will happen if i'll need to change the function name, or remove it completely, in angular i'll have to go over the entire code and remove the ng-clicks.

like image 468
emc Avatar asked Jan 30 '26 03:01

emc


1 Answers

You can use ng-repeat

In JS

$scope.spans = ["Box 1", "Box 2", "Box 3"]

In HTML

<span ng-repeat="s in spans" ng-click="doSomething()">{{s}}</span>
like image 171
Arun Ghosh Avatar answered Jan 31 '26 17:01

Arun Ghosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!