Type checking in Angular 2 templates

后端 未结 7 538
夕颜
夕颜 2021-02-02 06:07

We are building an application in Angular 2 and TypeScript. We try to statically check types where it is possible. Is there any way to check types in templates? Consider the fol

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 06:19

    You could use a Pipe:

    export class ObjectTypePipe implements PipeTransform {
        transform(value: any, args?: any): string {
            if (value != undefined && value != null) {
                return value.constructor.name.toString();
            }
            return "";
        }
    }
    

    This would then allow you to do a string comparison.

提交回复
热议问题