2017-04-05 52 views
1

我在这里找到答案,所有的时间,但还没有张贴在多年的问题。我已经阅读了类似于当前版本的问题,但是在应用了我找到的所有提示后,仍然无法让我的代码正常工作。我正在尝试使用ng-class有条件地指定一个类,但它不起作用。角:无法获得纳克级在里面工作NG-重复

这些都是非常大的文件(所以我就交相关下面的代码),它们除了试图添加NG-类angularJS伟大的工作。我已将按钮文本作为选定属性的布尔值,以确保它正确切换,并且它确实如此。只是不适用于ng级。

在此先感谢!

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:

.testing {  color: red; } 

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; 
    } 
+1

似乎是工作的罚款。我的猜测是你错过了在CSS中定义类。 – Lex

+0

@Lex不,这是肯定在我的CSS文件。它出现在我在Chrome开发人员工具中看到的css文件中,所以我知道它在服务器上。我甚至插入了其他我认为在代码的其他地方工作的css类,以查看他们是否能够使用这个ng类,但他们不知道。这是在吹我的脑海。 –

回答

0

好,知道了它的工作!发现问题是我使用的第三方CSS文件的颜色标有“!important”,所以这就是为什么我的CSS无法覆盖它。我现在标记了班级的颜色“!重要”,因此它正常工作。记得检查下一次我有风格类问题!感谢大家的帮助!

0

我创建了一个演示,它似乎是工作的罚款。由于这是较大代码的一小部分,因此请确保相应的样式类已正确添加。

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>

+2

你做了什么改变? –

+0

感谢您这样做,@sachilaranawaka。但是在我的其他代码中,它仍然不适用于我 - 但是,如果没有这个补充,我的代码其余部分都可以正常工作。当我尝试运行这个工具时,我绝对不会在开发人员工具中发现任何错误,所以我不知道发生了什么。 –