javascript - 如何在 ng-repeat 中添加类

标签 javascript css angularjs angular-ui-bootstrap

这是我的 Html 代码:

<div ng-controller="MainController">
  <ul class="list-holder">
     <li ng-repeat="check in checks" ng-class="{active : isSelected(check)}">
        <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
        <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
        <button ng-click="setcolor(check)">{{check.name}} </button> &nbsp;
        <hr>
    </li>
  </ul>
  <hr>
   {{selected | json}}
</div>

这是我的 Controller

function MainController($scope) {
    $scope.checks = [
       {name: 'sports'},
       {name: 'movies'},
       {name: 'Others'}
    ];

    $scope.setcolor = function(chk) {
       $scope.selected = chk;
    }

    $scope.isSelected = function(check) {
       return $scope.check === check;
    }
}

这是我的 CSS 文件

.active {
   background-color: red;
 }

我想使用切换功能在每一行中选择一个按钮。假设在第一行中选择了按钮 1,在第二行中选择了按钮 2,在第三行中选择了按钮 1。

最佳答案

看起来你在 isSelected 函数中有一点错误。您想与 $scope.selected 属性进行比较:

$scope.isSelected = function(check) {
    return $scope.selected === check;
}

关于javascript - 如何在 ng-repeat 中添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26254534/

相关文章:

javascript - 如何正确输入这个 `makeCancellable`函数并使Flowtype停止提示?

javascript - 如何将 MutationObserver 与对象一起使用?

javascript - 如何评估第一个快照

css - 更改动画背景的渐变颜色

css - AngularJS Material 日期选择器未对齐

json - 检测 AngularJS $http.post() 调用中格式错误/无效的 JSON

javascript - Angular ui-Grid Splice 不适用于数组

javascript - 空检查对象不起作用

php - <noinclude> 类别 :example</noinclude> in template, 意外地将内容包装在 <pre> 标记中

html - IE8 无法打印动态元素