如何在Angular的数据类型上使用ngSwitch? [英] How to use ngSwitch on datatype in angular?
本文介绍了如何在Angular的数据类型上使用ngSwitch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在angular2中工作,想知道当变量具有特定数据类型时是否可以使用ngSwitch加载<div>
标签.
像这样的东西:
I was working in angular2 and was curious to know whether I could use ngSwitch to load <div>
tag when variable is of certain datatype.i.e.
something like this:
<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
标签?
如果没有,对此有什么解决方法?
is this possible to load the div
tag when variable is of certain datatype?
if not, any workaround for this?
推荐答案
一种替代方法是使用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>
这篇关于如何在Angular的数据类型上使用ngSwitch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文