I am developing a test application in angular 2 and I'm stuck with a problem of adding classes based on list from model.
In Angular 1 one could do:
// model $scope.myClasses = ['class1', 'class2', ...]; // view ... ng-class="myClasses" ...
In Angular 2, all I have been able to do so far is:
// view ... [class.class1]="true" [class.class2]="true" ...
Which is obviously not very dynamic and I'm sure there must be a better way to do this.
However, I have also tried:
// model class ... { private myClasses: any; constructor() { this.myClasses = ['class1', 'class2', ...]; } // view ... [class]="myClasses" ...
but this doesn't work, I have tried myClasses
as a string name of a single class, array of strings, object with a classname key and true as a value, an array of objects of this kind, but sadly, nothing of listed will work this way.
Definition and Usage. The ng-class directive dynamically binds one or more CSS classes to an HTML element. The value of the ng-class directive can be a string, an object, or an array. If it is a string, it should contain one or more, space-separated class names.
To add a conditional class in Angular we can pass an object to ngClass where key is the class name and value is condition i.e., true or false as shown below. And in the above code, class name will be added only when the condition is true.
You can use both class and ngClass as the first one gives you the opportunity to apply a class that you want to implement in all cases under any circumstances and the later to apply classes conditionally.
javascript - ng-if and ng-class-even/odd doesn't work well together and won't get expected outout - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
According to the NgClass API docs, Angular 2 will accept a string, an Array, or an Object/map as the expression to NgClass. Or, of course, you could specify a function that returns one of those.
import {Component, CORE_DIRECTIVES} from 'angular2/angular2' @Component({ selector: 'my-app', directives: [CORE_DIRECTIVES], template: ` <div> <h2>{{title}}</h2> <div ngClass="gray-border purple">string of classes</div> <div [ngClass]="'gray-border purple'">string of classes</div> <div [ngClass]="['gray-border', 'green']">array of classes</div> <div [ngClass]="{'gray-border': true, 'blue': true}">object/map of classes</div> <button [ngClass]="{active: isActive}" (click)="isActive = !isActive">Click me</button> </div> `, styles: [` .gray-border { border: 1px solid gray; } .blue { color: blue; } .green { color: green; } .purple { color: purple; } .active { background-color: #f55; } `] }) export class App { title = "Angular 2 - NgClass"; isActive = false; }
Plunker
If you are passing a literal string, note the two alternative syntaxes:
<div ngClass="gray-border purple">string of classes</div> <div [ngClass]="'gray-border purple'">string of classes</div>
I believe the first is just syntactic sugar for the second.
And to quote the docs:
While the NgClass directive can interpret expressions evaluating to string, Array or Object, the Object-based version is the most often used and has an advantage of keeping all the CSS class names in a template.
You must specify CSSClass
in directives
property of @View
decorator. Check out this plunk.
@Component({ selector: 'app', }) @View({ template: '<div [class]="classMap">Class Map</div>', directives: [CSSClass] }) class App { constructor() { this.classMap = { 'class1': true, 'class2': false }; setInterval(() => { this.classMap.class2 = !this.classMap.class2; }, 1000) } }
UPD
CSSClass
was renamed to NgClass
in alpha-35. See this plunk
@Component({ selector: 'app', }) @View({ directives: [NgClass], template: ` <div [ng-class]="classMap">Class Map</div> `, }) class App { /* ... */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With