从ng-repeat定位$ index

时间:2015-06-12 16:58:34

标签: angularjs controller angularjs-scope

我如何在控制器中定位此模型?

formData[$index].ID

这不起作用 -

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

1 个答案:

答案 0 :(得分:1)

您实际上会从模板中注入对象或索引。像这样:

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

然后在你的代码中:

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

或者,您可以注入整个对象,而不是注入索引:

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

然后在你的代码中:

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