如果我为选择元素定义的 ngModel 不为空,如何将类添加到页面上的元素。目前有这个..
<md-select placeholder="Location" name="location" [(ngModel)]="selectedLocation">
<md-option *ngFor="let option of locationOptions" [value]="option.value">
{{option.viewValue}}
</md-option>
</md-select>
如果 ngModel 不为空,则想向 span 元素添加一个类,类似这样...
<p>Location:<span ng-class="{'complete': selectedLocation.length > 0}">{{ selectedLocation }}</span></p>
最佳答案
使用[ngClass]
<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>
关于javascript - Angular 4 - 如果 ngModel 不为空则添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45889205/