0
myController的:
$scope.items = [
{name: 'item 1', description: 'desc001'},
{name: 'item 2', description: 'desc002'},
{name: 'item 3', description: 'desc003'},]
$scope.selectRow = function (index) {
$scope.selectedRow = index;
}
CSS:
.select {
background-color: lightgreen;
}
HTML 1:
<div ng-controller="myController">
<table>
<tr ng-repeat="item in items" ng-click='selectRow($index)' ng-class="{select:$index == selectedRow}">
<td>{{item.name}}</td>
<td>{{item.description}}</td>
</tr>
</table>
HTML 2:
<div ng-controller="myController">
<table>
<tr ng-repeat="item in items" ng-click='selectedRow = $index' ng-class="{select:$index == selectedRow}">
<td>{{item.name}}</td>
<td>{{item.description}}</td>
</tr>
</table>
为什么HTML-1的作品,但HTML-2无法正常工作? 他们之间有什么不同?
NG-重复会为每个元素的新范围,当你在你的第二个例子做'selectedRow = $ index',它的工作原理,但只有上线的电流范围,而不是在你需要它的父范围,更明确地,你可以做'$ parent.selectedRow = $ index'这将工作,但不是真正的优化看着它,最好的办法这里是你的第一个 – DotDotDot
你已经选择行控制器predfined?林不记得是这种情况下需要这种 – makallio85
DotDotDot是正确的! – makallio85