如何在控制器中传递或引用$ index

时间:2015-10-15 10:06:49

标签: angularjs

在视图(html文件)中我有:

<tr ng-repeat="task in ctrl.model.unplannedTasks">
   <td>{{::$index + 1}}</td>
</tr>  

如何在控制器中引用$index + 1值?

我试过这样做:

<tr ng-repeat="task in ctrl.model.unplannedTasks" ng-init="myIndex =   $index + 1">
   <td ng-model = "myIndex">{{myIndex}}</td>
</tr>

然后使用myIndex在控制器中引用它,但它是undefined

[编辑]
$index + 1是表中的行号。我想引用特定行的当前值,以便能够将其放在消息中,如:
您确定要删除行吗? 5? (此消息在控制器内生成)

2 个答案:

答案 0 :(得分:0)

在控制器中,如果它用作值或以其他方式用作数组:

angular.module('rock').controller('test',['$scope',function($scope){
$scope.myIndex=0;
$scope.unplannedTasks="you can define your object here";
}]);

In view:
<tr ng-repeat="task in unplannedTasks">
   <td myIndex={{::$index + 1}}></td>
</tr>

答案 1 :(得分:0)

我只需要将$index+1传递给生成消息的函数。

<tr ng-repeat="task in ctrl.model.unplannedTasks">
<td>{{::$index + 1}}</td>
</tr>  
...
<button type="button" class="btn btn-primary btn-xs" ng-click="ctrl.openModal(task, $index + 1)" title="{{::msg.common.button.remove}}">{{::msg.common.button.remove}}</button>