Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng repeat中的ng click='selectRow($index)'和ng click='selectedRow=$index'有什么不同_Javascript_Html_Angularjs - Fatal编程技术网

Javascript ng repeat中的ng click='selectRow($index)'和ng click='selectedRow=$index'有什么不同

Javascript ng repeat中的ng click='selectRow($index)'和ng click='selectedRow=$index'有什么不同,javascript,html,angularjs,Javascript,Html,Angularjs,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: HTML 1: <d

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:

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单击class='selectRow$index'将调用名为selsectRow的函数,并将该行的索引传递给它


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

ng repeat为每个元素创建一个新的作用域,当您在第二个示例中执行selectedRow=$index时,它将工作,但仅在行的当前作用域上,不是您需要的父范围,为了更清楚地观察它,您可以执行$parent.selectedRow=$index,这将起作用,但不是真正优化的,这里最好的方法是您的第一个方法您是否在控制器中选择了预先定义的行?如果lke ThisDotDot是正确的,我记不起这是必需的!
<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>