如何在 Angular2中检查 ngIf 中的变量类型

我正在学习 Angular2。我有一个带有变量的组件,它是一个对象。 我在对象的字段上迭代,根据该位置的数据类型,我需要呈现一个不同的组件。 在这种情况下,我想渲染的 label,如果 typeof,该位置是一个 number,但这是不工作的

<div>
<div *ngIf='obj'>
<label *ngFor="let key of keys; let i = index">
<label class='key'>{{key}}:</label>
<label class='number' *ngIf='typeof obj[key] === "number"'>
<!-- label class='number' *ngIf='obj[key] | typeof === "number"' -->
{{ obj[key] }}
</label>
</label>
</div>
</div>

有什么想法吗?

我还创建了一个管道来获取 typeof,它在我打印值时起作用,但是在 * ngIf 中不起作用

116322 次浏览

Globals like window, typeof, enums, or static methods are not available within a template. Only members of the component class and typescript language constructs are available.

You can add a helper method to your component like

isNumber(val): boolean { return typeof val === 'number'; }

and use it like

<label class='number' *ngIf='isNumber(obj[key])'>

This is a bit of a hack, but if you need to do this in a lot of places and don't want the cruft of passing some isNumber function around, there's another option that can work if you use it carefully.

You can check for the existence of properties or methods that exist on the prototype of the object or type you're looking for. For example, all numbers have a toExponential function, so:

<label class='number' *ngIf='obj[key] && obj[key].toExponential'>

For functions you could look for call, for strings you could look for toLowerCase, for arrays you could look for concat, etc.

This approach isn't foolproof at all, since you could have an object that happens to possess a property with the same name that you're checking (though if the property you're checking is all you need, then we're basically duck typing), but if you know that the value you have is a primitive you're in good shape, since you can't assign properties on primitives (here is some interesting reading on that topic).

Disclaimer: I don't really trust that this is a good idea and may not be very maintainable or portable, but if you just need something quick for a prototype or a very limited use case, this is a reasonable tool to have in your belt.

Alternatively, you can compare the constructor name.

\{\{ foo.constructor.name === 'FooClass' }}

Detailed info about this here.

I just tried this and found it won't work in production because function names are shortened. It's safer to use something like:

foo instanceof FooClass

But note that you have to do this in the component/directive because instanceOf is not available in templating:

// In your component
isFoo(candidate){
return candidate instanceof FooClass;
}


// in your template
\{\{isFoo(maybeFoo)}}

You can create simple pipe which will receive current item and return item type.

import {Pipe, PipeTransform} from '@angular/core';


@Pipe({
name: 'typeof'
})
export class TypeofPipe implements PipeTransform {


transform(value: any): any {
console.log("Pipe works ", typeof value);
return typeof value;
}


}

Now you can use typeof pipe in html like this

*ngIf = (item | typeof) === 'number'

And be careful in using function call in your html as mentioned above. It preferred to use pipe instead of function call. Here is Stackblitz example. In first case function call will triggered on any change detection (example: clicking on buttons).