简体   繁体   English

ng-repeat中的ng-click ='selectRow($ index)'和ng-click ='selectedRow = $ index'有什么区别

[英]What's the different between ng-click='selectRow($index)' and ng-click='selectedRow = $index' in ng-repeat

myController: 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: CSS:

.select {
        background-color: lightgreen;
    }

HTML 1: 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: 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>

Why the HTML-1 works but HTML-2 not work correctly? 为什么HTML-1可以工作,但HTML-2不能正常工作? what's the different between them? 他们之间有什么区别?

ng-click='selectRow($index)' will call function named selsectRow() and pass it the index of the row. ng-click='selectRow($index)'将调用名为selsectRow()的函数,并将行的索引传递给该函数。

ng-class="{select:$index == selectedRow} this expression will change the name of the class of this directive based on the result of the boolean operation $index == selectedRow . ng-class="{select:$index == selectedRow}该表达式将根据布尔操作$index == selectedRow的结果更改此指令的类的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM