Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat passing index value to a function

I need to pass a $index value of a specific element, added with ng-repeat, to a javascript function. My code sample:

<tr ng-repeat="cells in CouponsList.CellPhones">
<td><button  ng-click="doStuff($index+1)">{{cells.localVendorAddress}}</button></td>

Now I am adding several buttons and when a specific button is pressed I need to send it's specific $index to the doStuff($index) function. Any idea?

like image 329
KBE Avatar asked Jul 21 '14 19:07

KBE


People also ask

How do you pass an index in NG-repeat?

Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope. So what you need to do is reach up to the parent scope, and use that $index . Save this answer.

What can I use instead of NG-repeat?

But ng-repeat is not the right thing to use when you have large datasets as it involves heavy DOM manipulations. And you should consider using ng-repeat with pagination. You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

What does ng-repeat do?

The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

How do I filter in NG-repeat?

The ng-repeat values can be filtered according to the ng-model in AngularJS by using the value of the input field as an expression in a filter. We can set the ng-model directive on an input field to filter ng-repeat values.


3 Answers

please see here : http://jsbin.com/yeweh/4/edit

<a href="" ng-repeat="s in students" ng-click="doit($index)">{{s.name}} - {{s.class}} </a>


var app = angular.module('app', []);



app.controller('firstCtrl', function($scope){

  $scope.doit= function(index){
    alert(index)

  }

  $scope.students = [
           {name: 'Aa_Student', class: 'A_Class'},
           {name: 'Ab_Student', class: 'A_Class'},
           {name: 'Ac_Student', class: 'B_Class'},
           {name: 'Ba_Student', class: 'B_Class'},
           {name: 'Bb_Student', class: 'C_Class'},
           {name: 'Bc_Student', class: 'C_Class'}];
});
like image 189
sylwester Avatar answered Oct 13 '22 09:10

sylwester


Here is an example.

http://jsfiddle.net/bdmRs/

What you have there looks ok but you might be missing something with the function definition for doStuff. It must be defined on the $scope like this:

$scope.doStuff = function(idx){
    alert(idx);
};
like image 41
Hippocrates Avatar answered Oct 13 '22 09:10

Hippocrates


You need to make sure when you're using ng-click that the function that is called is declared on the scope in the controller.

Therefore, if your function name is doStuff(index), you should see this defined somewhere in your code as (the below makes assumptions about your module and controller names):

var app = angular.module("MyModule", []);
app.controller("MyController", function($scope){

    $scope.doStuff = function(index){
        ... 
    }

}
like image 45
rchawdry Avatar answered Oct 13 '22 08:10

rchawdry