【问题标题】:*ngFor not executing when using variable from the module in ionic/angular project*ngFor 在 ionic/angular 项目中使用模块中的变量时不执行
【发布时间】:2019-08-14 10:10:15
【问题描述】:

我正在创建一个 Ionic/Angular 应用程序,我想在使用 *ngFor 的语句中显示列表中的值。但由于某种原因,*ngFor 什么也没做。

*ngFor 在我说*ngFor="let i of eventList" 时不会运行,但是当我将变量的值放在里面时会运行

*ngFor="let i of {'id': 1, 'name':'Superman'},{'id' :2,'name':'Batman'},
  {'id' : 5, 'name':'BatGirl'},
  {'id' : 3, 'name':'Robin'},
  {'id' : 4, 'name':'Flash'}
];"

然后它运行。

这是我的 tab1.module.ts 和 html

import { RouterModule } from '@angular/router';
import { NgModule, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Tab1Page } from './tab1.page';
import { AuthService} from '../auth.service';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    HttpClientModule,
    RouterModule.forChild([{ path: '', component: Tab1Page }])
  ],
  declarations: [Tab1Page]
})
export class Tab1PageModule
{ 
  public eventList: {'id': number, 'name': string}[]  =[{'id': 1, 'name':'Superman'},
  {'id' : 2, 'name':'Batman'},
  {'id' : 5, 'name':'BatGirl'},
  {'id' : 3, 'name':'Robin'},
  {'id' : 4, 'name':'Flash'}
];
  public message: any = '';
  constructor(public auth: AuthService)
  { 
  } 
}

html

<ion-item >
  <ion-input type="number" clearInput></ion-input>
    <ion-select placeholder="Select One" >
      <ion-select-option *ngFor="let i of eventList" value="{{i.id}}">
          {{i.name}}
       </ion-select-option>
    </ion-select>
</ion-item>

当我运行它时,选择框保持为空。我也尝试了许多不同的方法和变量的格式,但它什么也没做。

【问题讨论】:

    标签: angular ionic-framework


    【解决方案1】:

    将代码放在您的组件中,而不是在模块中。 Angular modules主要用于进口、出口、报关等。

    【讨论】:

      【解决方案2】:

      你应该把你的逻辑放在你的Tab1PageComponent而不是模块本身上。

      该模块用于声明依赖项、导出组件等。它几乎总是保持为空。

      【讨论】:

      • 一个愚蠢的错误。我没有看到有 tab1.page.ts 文件。谢谢你好先生@Elias Soares
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 2020-02-19
      • 2017-11-10
      • 1970-01-01
      • 2020-11-02
      相关资源
      最近更新 更多