Javascript 角度4-如果ngModel不为空,则添加类
如果为select元素定义的ngModel不是空的,如何向页面上的元素添加类。目前有这个Javascript 角度4-如果ngModel不为空,则添加类,javascript,html,angular,if-statement,material-design,Javascript,Html,Angular,If Statement,Material Design,如果为select元素定义的ngModel不是空的,如何向页面上的元素添加类。目前有这个 <md-select placeholder="Location" name="location" [(ngModel)]="selectedLocation"> <md-option *ngFor="let option of locationOptions" [value]="option.value"> {{option.viewValue}}
<md-select placeholder="Location" name="location" [(ngModel)]="selectedLocation">
<md-option *ngFor="let option of locationOptions" [value]="option.value">
{{option.viewValue}}
</md-option>
</md-select>
{{option.viewValue}}
如果ngModel不是空的,我想给span元素添加一个类,类似这样的
<p>Location:<span ng-class="{'complete': selectedLocation.length > 0}">{{ selectedLocation }}</span></p>
位置:{{selectedLocation}
我不知道angular4,但在Angular1中是这样的
<div ng-class="{'myClass':myModel}"></div>
它需要在括号之间才能绑定到变量。此外,您的ng class属性错误,它需要是
ngClass
<p>Location:<span [ngClass]="{'complete': selectedLocation.length > 0}">{{ selectedLocation }}</span></p>
位置:{{selectedLocation}
使用[ngClass]
作为
。。。
这里您有一个使用[class]的案例,因为您只有一个类可以应用:
<p>Location:<span [class.complete]="selectedLocation.length > 0">{{ selectedLocation }}</span></p>
位置:{{selectedLocation}
我想要短得多。用
[ngClass]
代替。检查文档https://angular.io/api/common/NgClass
我没能将我的类放在单引号下,试试看。如果仍然不起作用,那么只知道angular4的人可以帮助您当我尝试上面的方法时,我正在控制台中收到此消息…'无法绑定到'ng class',因为它不是'span'的已知属性
<p>Location:<span [class.complete]="selectedLocation.length > 0">{{ selectedLocation }}</span></p>