轉自:https://blog.csdn.net/jumtre/article/details/50802136javascript
其餘博文ng-class使用方法:https://blog.csdn.net/sinat_36146776/article/details/78340922html
有三種方法:java
一、經過$scope綁定(不推薦)數組
二、經過對象數組綁定app
三、經過key/value鍵值對綁定oop
實現方法:spa
一、經過$scope綁定(不推薦):.net
function ctrl($scope) { $scope.className = "selected"; }
<div class="{{className}}"></div>
二、經過對象數組綁定:code
function ctrl($scope) { $scope.isSelected = true; }
<div ng-class="{true:'selected',false:'unselected'}[isSelected]"></div>
三、經過key/value鍵值對綁定:htm
function ctrl($scope) { $scope.isA = true; $scope.isB = false; $scope.isC = false; }
<div ng-class="{'A':isA,'B':isB,'C':isC}"></div>
當isA爲true時,增長A樣式;當isB爲true時,增長B樣式;當isC爲true時,增長C樣式。
<ion-list> <ion-item ng-repeat="project in projects" ng-click="selectProject(project, $index)" ng-class="{active: activeProject == project}"> {{project.title}} </ion-item> </ion-list>根據projects循環建立ion-item,當activeProject爲當前循環到的project時,增長active樣式。
幾點說明:
一、不推薦第一種方法,由於controller $scope應該只有數據和行爲
二、ng-class是增長相關樣式,能夠和class同時使用