问题
Just curious if there are any bigger advantages on any of these approaches?
<div class="small" ngClass="well-{{small}}">small</div>
or
<div class="small" class="well-{{big}}">big</div>
回答1:
The way you are using ngClass
it does not matter. Although, ngClass allows us to conditionally add classes, i.e.
<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
when using class
, you will have something like:
<some-element [class]="someExpression ? 'first' : ''">...</some-element>
To sum it up, ngClass is more flexible when you have to add conditional classes.
回答2:
The only advantage in my experience is when you want to format the data. For example a date, otherwise it is the same:
{{'1288323623006' | date:'MM/dd/yyyy @ h:mma'}}
回答3:
*If you want to add class
, use class
as below.
class="className"
*If you want to add one class
conditionally, you can use class/ngClass in angular
as below.
[class.className]="condition"
[ngClass]="{'className': condition}"
*If you want to add more than one class
conditionally, you have two way.
Way 1:
Use class in angular
as below:
[class.className1]="condition1"
[class.className2]="condition2"
.
.
.
Way 2:
Use ngClass in angular
as below:
[ngClass]="{'class1': condition1 , 'class2': condition2 ... }"
And clearly the 2nd way is better than the 1st. Phew !!
来源:https://stackoverflow.com/questions/45803967/ngclass-or-class