Nogga Nogga - 1 year ago 184
AngularJS Question

Angular ng-click changes all array elements

I got a array of objects in my scope.
Additionally I got a button that I would like to equip with a ng-click routine, that changes a certain attribute of every object in the array.

Can I do that all in a one-liner in ng-click? Alternative, I can also equip my scope with a function and pass on the array, that I would like to adjust all elements in.



{"languages": [
"hide": false,
"title": "Deutsch",
"level": 7
"hide": false,
"title": "Englisch",
"level": 6
"hide": false,
"title": "Französisch",
"level": 2

And my respective button (with a pseudo function at the moment):

<a class="button" href="#" ng-click="angular.forEach($scope.languages, function (item, index) {item.hide=true});">Alle ausblenden</a>

Answer Source

Put that in a function of the scope. Keep your view as simple as possible. Put JS code in the controller so that you can unit test it, document it, format it.

And you don't even need to pass the array, since it's in the scope:


Isn't that much more readable?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download