简体   繁体   English

Angular:无法让ng-class在ng-repeat中工作

[英]Angular: Can't get ng-class to work inside ng-repeat

I find answers here all the time, but haven't posted a question in years. 我一直在这里找到答案,但多年来一直没有发表问题。 I've read through questions similar to my current one, but after applying all the tips I've found, I still can't get my code to work. 我已经阅读了类似于我当前的问题,但在应用了我发现的所有提示之后,我仍然无法让我的代码工作。 I am trying to assign a class conditionally using ng-class and it's not working. 我试图使用ng-class有条件地分配一个类,它不起作用。

These are all very large files (so I'll just post relevant code below) that are working great with angularJS except for trying to add ng-class. 这些都是非常大的文件(因此我将在下面发布相关代码),除了尝试添加ng-class之外,它们与angularJS一起工作得很好。 I've put the button text as the boolean value for the selected property to make sure that it toggles correctly, and it does. 我将按钮文本作为所选属性的布尔值,以确保它正确切换,并且确实正确切换。 Just doesn't work for ng-class. 只是不适用于ng级。

Thanks in advance! 提前致谢!

HTML: HTML:

<div>
    <div><em>Click buttons below to show or hide bills for each issue.</em></div>
    <button ng-repeat="type in issueTypes" ng-click="toggleBtn(type.show, $index)" 
            class="w3-button w3-round-small w3-light-blue btn-space" 
            ng-class="{'testing' : type.selected}">{{type.selected}}</button>   
</div>

CSS: CSS:

 .testing {     color: red; }

JS: JS:

    $scope.issueTypes = [{name: "Education", show: "education", selected: false}, {name: "Health Care", show: "health", selected: false},  
    {name: "Civil Rights", show: "civil", selected: true}, {name: "Gun Control", show: "guns", selected: false}, {name: "Women's Rights", show: "women", selected: false}];

    $scope.toggleBtn = function(showString, index) {
    $scope[showString] = !$scope[showString];
    $scope.issueTypes[index].selected = !$scope.issueTypes[index].selected;
  }

I create a demo and it seems to be working fine. 我创建了一个演示,它似乎工作正常。 Since this is a small part of the larger code, make sure the relevant style classes are properly added. 由于这只是较大代码的一小部分,因此请确保正确添加相关样式类。

 angular.module("app",[]) .controller("ctrl",function($scope){ $scope.issueTypes = [{name: "Education", show: "education", selected: false}, {name: "Health Care", show: "health", selected: false}, {name: "Civil Rights", show: "civil", selected: true}, {name: "Gun Control", show: "guns", selected: false}, {name: "Women's Rights", show: "women", selected: false}]; $scope.toggleBtn = function(showString, index) { $scope.issueTypes[index].selected = !$scope.issueTypes[index].selected; } }) 
 .testing { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <div> <div><em>Click buttons below to show or hide bills for each issue.</em></div> <button ng-repeat="type in issueTypes" ng-click="toggleBtn(type.show, $index)" class="w3-button w3-round-small w3-light-blue btn-space" ng-class="{'testing' :type.selected}">{{type.selected}}</button> </div> </div> 

Okay, got it working! 好的,搞定了! Found that the problem was the 3rd party CSS file I was using had their color marked "!important", so that was why my CSS wasn't able to override it. 发现问题是我使用的第三方CSS文件的颜色标记为“!important”,所以这就是为什么我的CSS无法覆盖它。 I've now marked my class's color "!important" and so it is working properly. 我现在标记了我班级的颜色“!important”,所以它正常工作。 Got to remember to check for that next time I'm having a style class issue!! 下次我有风格课问题时要记得检查一下! Thanks for everyone's help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM