dave
dave

Reputation: 355

Targeting $index from ng-repeat

How would I go about targeting this model in the controller?

formData[$index].ID

This is not working -

$scope.getJob = function() {
        Employee.get({id: "" + $scope.formData[$index].ID}, function (data) {
            console.log(data);
        })
    }

Upvotes: 1

Views: 45

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

You would actually inject the object or index from your template. Something like this:

<div ng-repeat="o in formData">
    <button ng-click="getJob($index)">Click me!</button>
</div>

and then in your code:

$scope.getJob = function(idx) {
    Employee.get({id: "" + $scope.formData[idx].ID}, function (data) {
        console.log(data);
    })
}

Alternatively, instead of injecting the index, you could inject the entire object:

<div ng-repeat="o in formData">
    <button ng-click="getJob(o)">Click me!</button>
</div>

and then in your code:

$scope.getJob = function(o) {
    Employee.get({id: o.ID}, function (data) {
        console.log(data);
    })
}

Upvotes: 1

Related Questions