There are three methods:
1. Binding through $scope (not recommended)
2. Binding through object array
3. Binding through key/value pairs
Implementation method:
1. Through $scope Binding (not recommended):
function ctrl($scope) { $scope.className = "selected"; }
<div class="{{className}}"></div>
2. Binding through object array:
function ctrl($scope) { $scope.isSelected = true; }
<div ng-class="{true:'selected',false:'unselected'}[isSelected]"></div>
When isSelected is true, increase the selected style; when isS When elected is false , add unselected style.
3. Binding through key/value pairs:
function ctrl($scope) { $scope.isA = true; $scope.isB = false; $scope.isC = false; }
<div ng-class="{'A':isA,'B':isB,'C':isC}"></div>
When isA is true, add style A; when isB is true, add style B; when isC is true, Add C style.
<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>
Create ion-item based on the projects loop. When the activeProject is the project currently looped to, add the active style.
Some notes:
1. The first method is not recommended because controller $scope should only have data and behavior
2. ng-class adds related styles and can be used together with class