简体   繁体   English

如何循环离子2上的对象数组?

[英]How to loop on an Array of Objects on ionic 2?

I have an array of objects on my application TS file I'm populating my array with an object that I'm getting from the storage. 我的应用程序TS文件中有一个对象数组我正在使用我从存储中获取的对象填充数组。

import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar, Splashscreen } from 'ionic-native';
import { Dbservice } from '../providers/dbservice';

import { Home } from '../pages/page1/page1';
 import { Setup } from '../pages/frameSetup/setup';

export class Home {
    deviceObj = [];
     constructor(){
         this.deviceObj.push(otherObject);
     }
}

on my html file I want to look on the array and display the data, but when I do this I get an error message 在我的html文件中,我想查看数组并显示数据,但是当我这样做时,我收到一条错误消息

Here is the code on my html file. 这是我的html文件上的代码。

  <ion-row>
    <ion-col width-10 *ngFor="let x of deviceObj">
      <button ion-button outline icon-only small>
        <ion-icon name="ios-add"></ion-icon>
      </button>
    </ion-col>
    <ion-col width-10>
      <button ion-button outline icon-only small [navPush]="setupPage">
        <ion-icon name="ios-add"></ion-icon>
      </button>
    </ion-col>
  </ion-row>

I get an error saying that : 我得到一个错误说:

polyfills.js:3 Error: Uncaught (in promise): Error: Template parse errors: Can't bind to 'ngforOf' since it isn't a known property of 'ion-col'. polyfills.js:3错误:未捕获(在承诺中):错误:模板解析错误:无法绑定到'ngforOf',因为它不是'ion-col'的已知属性。 1. If 'ion-col' is an Angular component and it has 'ngforOf' input, then verify that it is part of this module. 1.如果'ion-col'是一个Angular组件并且它有'ngforOf'输入,那么请确认它是该模块的一部分。 2. If 'ion-col' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. 2.如果'ion-col'是Web组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到此组件的“@NgModule.schemas”以禁止显示此消息。

You need to add BrowserModule to imports: [...] of AppModule if this is where your code is, or alternatively add CommonModule to imports: [...] if your code is in another module. 您需要将BrowserModule添加到imports: [...] AppModule如果这是您的代码所在,或者将CommonModule添加到imports: [...]如果您的代码在另一个模块中。

The error message means that the NgFor directive is not known in the module. 错误消息表示模块中未知NgFor指令。 Importing CommonModule or BrowserModule ( AppModule only) makes it available. 导入CommonModuleBrowserModuleAppModule )使其可用。

As the error says, you just can't use *ngFor in tag. 正如错误所说,你不能在标签中使用* ngFor。 Try using it inside tag and the error will disappear. 尝试在标签内使用它,错误将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM