typescript 如何在 Angular 2+ 模板中使用 isArray()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37678697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:33:44  来源:igfitidea点击:

How to use isArray() in Angular 2+ template?

javascriptangulartypescript

提问by Bhushan Gadekar

I have this variable named records,

我有这个名为的变量records

Now I want to check if its an array or not in angular2/typescript?

现在我想在 angular2/typescript 中检查它是否是一个数组?

In AngularJS I used to do Following:

在 AngularJS 中,我曾经这样做过:

ng-if="selectedCol.data && !isArray(selectedCol.data)"

But When I am trying to do the Following its not working;

但是当我尝试执行以下操作时,它不起作用;

*ngIf="selectedCol.model.data && !Array.isArray(selectedCol.model.data)"

Its giving me below error:

它给了我以下错误:

TypeError: Cannot read property 'isArray' of undefined any inputs?

类型错误:无法读取未定义任何输入的属性“isArray”?

回答by tchelidze

Angular 2 template is executed inside Component's context, meaning, you can only access properties/methods defined inside Component

Angular 2 模板在Component的上下文中执行,这意味着您只能访问内部定义的属性/方法Component

Simplest way is to define isArraymethod in your Component

最简单的方法是isArray在你的Component

isArray(obj : any ) {
   return Array.isArray(obj)
}

In template

在模板中

*ngIf="isArray(selectedCol.model.data)"

To avoid boilerplate code, define Service with isArraymethod, register as Singleton, inject into Componentand use isArraymethod via Service property

为了避免样板代码,用isArray方法定义服务,注册为单例,通过服务属性注入Component和使用isArray方法

Alternatively, define _arrayproperty in your Componentand assign Arraytype to it

或者_array在您的属性中定义属性Component并为其分配Array类型

  private _array = Array;

In template

在模板中

*ngIf="_array.isArray(selectedCol.model.data)"

回答by Estus Flask

While not being the most efficient solution (see the other answer), [].constructor.isArrayis suitable for any expression context and doesn't require to contaminate component classes with language-level helpers:

虽然不是最有效的解决方案(请参阅其他答案),但[].constructor.isArray适用于任何表达式上下文,并且不需要使用语言级帮助程序污染组件类:

*ngIf="selectedCol.model.data && [].constructor.isArray(selectedCol.model.data)"

回答by Abdullah Rasheed

In addition to what @tchelidze said:

除了@tchelidze 所说的:

Angular 2 provides a wrapper called isArrayin facade/langexported and defined like this:

Angular 2 提供了一个isArrayfacade/lang导出中调用的包装器,定义如下:

export function isArray(obj: any): boolean {
  return Array.isArray(obj);
}

You can import it into your component like this:

您可以像这样将其导入到您的组件中:

import {isArray} from '@angular/facade/lang';

Then you could expose it publicly in your component:

然后你可以在你的组件中公开它:

this.isArray = isArray

this.isArray = isArray

And use in your template like so:

并在您的模板中使用,如下所示:

*ng-if="selectedCol.data && !isArray(selectedCol.data)"

*ng-if="selectedCol.data && !isArray(selectedCol.data)"