2017-04-05 50 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。但是在我的其他代碼中,它仍然不適用於我 - 但是,如果沒有這個補充,我的代碼其餘部分都可以正常工作。當我嘗試運行這個工具時,我絕對不會在開發人員工具中發現任何錯誤,所以我不知道發生了什麼。 –