How to check type of variable in ngIf in Angular2

后端 未结 5 2056
鱼传尺愫
鱼传尺愫 2020-12-14 05:23

I\'m learning Angular2. I have a component with a variable which is an object. I\'m iterating over the fields of the object, and acording to the type of data of that positio

5条回答
  •  粉色の甜心
    2020-12-14 06:04

    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)}}
    

提交回复
热议问题