scope - AngularJS - Pass $index or the item itself via ng-click? -


say have

<div ng-repeat="item in scope.allitems">    <div ng-click="dosomething(...)"></div> </div> 

if want item upon click, 1 better option?

option 1

use ng-click="dosomething($index) , have:

$scope.dosomething = function($index) {     var myitem = $scope.allitems[$index]; } 

option 2

use ng-click="dosomething(item) , have:

$scope.dosomething = function(myitem) {     // whatever } 

if doing item, pass in. way function doesn't need know array item belongs to:

$scope.addcolor = function(car) {   car.color = 'red'; }; 

if, on other hand, need modify array prefer pass in $index , save having loop through array looking match:

$scope.deletecar = function(index, cars) {   cars.splice(index, 1); }; 

Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -