(AngularJS) 专门为 ng-repeat 中的一个元素设置 "ng-show" true
(AngularJS) Make a "ng-show" true specifically for one element in a ng-repeat
我从 Angular 开始,我想问的是是否有可能使 "ng-show" 对 "ng-repeat" 中的一个元素为真,并使 "ng-repeat" 为假其他人?
我用这段代码来解释自己:
html 文件:
<tbody ng-repeat="elem in List" ng-init="visible = false">
<tr ng-class="elemSelected(elem)">
<td colspan="3">
<a href ng-click="clickElemScroll(Elem)" >
<span class="glyphicon" ng-class="visible? 'glyphicon-chevron-down' : 'glyphicon-chevron-right'"></span>
<strong>{{elem.name}} </strong>
</a>
</td>
</tr>
<tr ng-show="visible" >
blabla
</tr>
</tbody>
js 文件:
$scope.clickElemScroll = function (elem) {
if ($scope.elemSelected === elem) {
$scope.visible = true
}
};
如你所知,问题是所有的元素都会滚动,如何让我点击的元素只可见?
您可以在要在 ngRepeat 中使用的元素上设置新的 属性。另外,不确定 ng-class 发生了什么,但我不建议在那里使用函数。如果您尝试在选择时设置 class,请尝试 ng-class="{'selected': elem.selected}"
<tbody ng-repeat="elem in List">
<tr ng-class="elemSelected(elem)">
<td colspan="3">
<a href ng-click="elem.selected = !elem.selected">
<span class="glyphicon" ng-class="visible? 'glyphicon-chevron-down' : 'glyphicon-chevron-right'"></span>
<strong>{{elem.name}} </strong>
</a>
</td>
</tr>
<tr ng-show="elem.selected">
blabla
</tr>
我从 Angular 开始,我想问的是是否有可能使 "ng-show" 对 "ng-repeat" 中的一个元素为真,并使 "ng-repeat" 为假其他人?
我用这段代码来解释自己:
html 文件:
<tbody ng-repeat="elem in List" ng-init="visible = false">
<tr ng-class="elemSelected(elem)">
<td colspan="3">
<a href ng-click="clickElemScroll(Elem)" >
<span class="glyphicon" ng-class="visible? 'glyphicon-chevron-down' : 'glyphicon-chevron-right'"></span>
<strong>{{elem.name}} </strong>
</a>
</td>
</tr>
<tr ng-show="visible" >
blabla
</tr>
</tbody>
js 文件:
$scope.clickElemScroll = function (elem) {
if ($scope.elemSelected === elem) {
$scope.visible = true
}
};
如你所知,问题是所有的元素都会滚动,如何让我点击的元素只可见?
您可以在要在 ngRepeat 中使用的元素上设置新的 属性。另外,不确定 ng-class 发生了什么,但我不建议在那里使用函数。如果您尝试在选择时设置 class,请尝试 ng-class="{'selected': elem.selected}"
<tbody ng-repeat="elem in List">
<tr ng-class="elemSelected(elem)">
<td colspan="3">
<a href ng-click="elem.selected = !elem.selected">
<span class="glyphicon" ng-class="visible? 'glyphicon-chevron-down' : 'glyphicon-chevron-right'"></span>
<strong>{{elem.name}} </strong>
</a>
</td>
</tr>
<tr ng-show="elem.selected">
blabla
</tr>