i在Angular2工作,很好奇知道当变量是某些datatype.i.e时,我是否可以使用ngswitch加载 <div>
标记。这样的东西:
<div [ng-switch]="value">
<p *ng-switch-when="isObject(value)">This is Object</p>
<p *ng-switch-when="isArray(value)">This is Array</p>
<p *ng-switch-when="isBoolean(value)">This is Boolean</p>
<p *ng-switch-when="isNumber(value)">This is Number</p>
<p *ng-switch-default>This is Simple Text !</p>
</div>
当变量具有某些数据类型时,这是否可以加载div
标签?如果没有,则有任何解决方法?
另一种选择是使用 ngIf
:
<p *ngIf="isObject(value)">This is Object</p>
<p *ngIf="isArray(value)">This is Array</p>
<p *ngIf="isBoolean(value)">This is Boolean</p>
<p *ngIf="isNumber(value)">This is Number</p>
<p *ngIf="!isObject(value) || !isArray(value) || !isBoolean(value) || !isNumber(value)">This is Simple Text !</p>
是的,您可以做到,但现在直接在模板中。只需在控制器中创建一个方法以检查一种类型:
import {Component} from '@angular/core'
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<div [ngSwitch]="checkType(name)">
<p *ngSwitchCase="'string'">is a string!</p>
<p *ngSwitchDefault>default</p>
</div>
</div>
`,
directives: []
})
export class App {
constructor() {
this.name = 'Angular2 (Release Candidate!)'
}
checkType(value) {
return typeof value
}
}
工作plunker
请先将您的角度更新到RC版本。