-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Closed
Description
Angular2 currently provide a way to bind css class as [class.fa-train]="true", it is great if you can also bind them at runtime, something like [class:variable]="true" or simply [class:variable].
Here is a simple example,
this.icons = ['fa-train', 'fa-subway', 'fa-bed']<i *ng-for="#icon of icons" class="fa" [class:icon]="true"></i><!--output-->
<i class="fa fa-train"></i>
<i class="fa fa-subway"></i>
<i class="fa fa-bed"></i>Here is an example with bootstrap and form. Before:
<div class="form-group" [class.has-success]="form.controls.name.valid && !database.duplicate" [class.has-warning]="form.controls.name.valid && database.duplicate" [class.has-error]="!form.controls.name.valid">
<input type="text" class="form-control" placeholder="Your name">
</div>After:
<div class="form-group" [class:nameValidity]="true">
<input type="text" class="form-control" placeholder="You name">
</div>get nameValidity () {
if (this.form.controls.name.valid && !this.database.duplicate) return 'has-success';
if (this.form.controls.name.valid && this.database.duplicate) return 'has-warning'
if (!this.form.controls.name.valid) return 'has-error';
}Thanks!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels