gpt4 book ai didi

javascript - 每个点击项目上的 "ng-class"

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:06 25 4
gpt4 key购买 nike

我需要在选定的 <li> 上添加一个类就像我在演示中展示的那样,但如果我点击另一个 <li> 不希望它被删除.

请帮忙

<div ng-app="myApp">
<ul ng-controller="myCtrl">
<li ng-repeat="item in model.items" class="commonClass" ng-class="{'on': model.selected==item}" ng-click="model.selected=item">{{ item.name }}</li>
</ul>
</div>

JS

var app = angular.module('myApp', []);

app.controller('myCtrl', function ($scope) {

$scope.model = {
selected: null,
items : [
{name: "Apple"},
{name: "Banana"},
{name: "California"}
]
};
})

demo

最佳答案

就这样添加

<div ng-app="myApp">
<ul ng-controller="myCtrl">
<li ng-repeat="item in model.items" class="commonClass" ng-class="{'on': selected==$index}" ng-click="selected=$index">{{ item.name }}</li>
</ul>
</div>

关于javascript - 每个点击项目上的 "ng-class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com