我有一张桌子,用引导程序设计。该表的内容是使用 Angular.js 填充的。如何使一行可点击,以便它将调用范围内的函数?
以下代码对我不起作用(ng-click 部分):
Table:
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="ingredient in ingredients" ng-click="setSelected({{$index}});">
<td>{{ ingredient.name }}</td>
<td>{{ ingredient.status }}</td>
</tr>
</tbody>
</table>
控制器:
$scope.setSelected = function(index) {
$scope.selected = $scope.ingredients[index];
console.log($scope.selected);
};
建议及fiddle http://jsfiddle.net/wnvSD/1/:
<tr ng-repeat="ingredient in ingredients" ng-click="setSelected();">
<td>{{ ingredient.name }}</td>
<td>{{ ingredient.status }}</td>
</tr>
<script>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.ingredients = [
{'name': 'potato'},
{'name': 'tomato'}
];
$scope.setSelected = function() {
$scope.selected = this.ingredient;
console.log($scope.selected);
};
}
</script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)