首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ng-repeat中的ng-click='selectRow($index)‘和ng-click='selectedRow = $index’有什么不同

ng-repeat中的ng-click='selectRow($index)‘和ng-click='selectedRow = $index’有什么不同
EN

Stack Overflow用户
提问于 2014-01-11 00:12:31
回答 1查看 113关注 0票数 0

myController:

代码语言:javascript
复制
$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:

代码语言:javascript
复制
.select {
        background-color: lightgreen;
    }

HTML 1:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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不能正常工作?它们之间有什么不同?

EN

回答 1

Stack Overflow用户

发布于 2014-03-07 18:07:36

ng-click='selectRow($index)'将调用名为selsectRow()的函数,并将行的索引传递给它。

ng-class="{select:$index == selectedRow}此表达式将根据布尔运算$index == selectedRow的结果更改此指令的类名。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21049092

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档