angular - 我正在开发一个 Angular WebApp,但我似乎无法让 *ngFor 工作
问题描述
老实说,我已经尝试了我所看到的所有内容,只导入了 app-routing.module 和 app.module,添加了 CommonModule 以及 ng build 和 reserve。有人请救救我,我已经用了 2 个小时了,这是我的 WebApp 的最后一部分,在此先感谢。
应用程序路由模块
import { NgModule, Component } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DisplayViewComponent } from './pages/display-view/display-view.component';
import { CommonModule } from '@angular/common';
const routes: Routes = [
{path: '', redirectTo:'RunninghillWordApp', pathMatch: "full"},
{path: 'RunninghillWordApp', component: DisplayViewComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes),CommonModule],
exports: [RouterModule]
})
应用程序模块
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { CommonModule } from '@angular/common';
import { DisplayViewComponent } from './pages/display-view/display-view.component';
@NgModule({
declarations: [
AppComponent, DisplayViewComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
CommonModule,
],
providers: [],
bootstrap: [AppComponent]
})
*edit 添加了我的 html 以防出现错误
<div class="history-display-container">
<!-- Display previous sentences -->
<a class="history-item" *ngFor="let item of history">
<p>{{item.text}}</p>
</a>
</div>
**编辑添加我的组件以防出现错误
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { SentenceService } from '../../services/sentence.service';
import { WordService } from '../../services/word.service';
import { Sentence } from '../../models/sentence.model';
import { Word } from '../../models/word.model';
@Component({
selector: 'app-display-view',
templateUrl: './display-view.component.html',
styleUrls: ['./display-view.component.scss']
})
export class DisplayViewComponent implements OnInit {
history: Sentence[];
words: Word[];
constructor(private sentenceService: SentenceService, private wordService: WordService, private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.params.subscribe(
(params: Params) => {
console.log(params);
}
)
//populate sentences and words arrays
this.sentenceService.getSentences().subscribe((sentences: Sentence[]) =>{
console.log(sentences);
this.history = sentences;
});
}
我得到的错误是
Can't bind to 'ngForOf' since it isn't a known property of 'a'.
**编辑重新启动我的浏览器和 VSCode 后,现在的错误是:
NgFor only supports binding to Iterables such as Arrays.
解决方案
根据您的评论,您正在{ sentences: [...] }
从您的服务接收对象,而不是数组。您需要更改接收结果的函数,然后设置本地history
属性。
//populate sentences and words arrays
this.sentenceService.getSentences().subscribe((result: any) =>{
this.history = [...result.sentences];
});
推荐阅读
- python - 在 Python 中附加特定列作为辅助 x 轴
- javascript - 如何根据声明要检查的属性的数组按不同属性过滤对象数组?
- python - How to get number of keys for a nested value in a dictionary?
- javascript - 返回数组的偶数和奇数元素
- google-apps-script - 使用 Google 表单创建 google 表格报告/布局,并根据表单选择添加额外内容
- android-studio - 使此类在任何方向扫描条形码需要进行哪些更改。它仅适用于二维码
- intel - 在 oneVPL 示例中,如何对不同分辨率的 mp4 文件进行编码?
- javascript - 如何在 Winjs UWP 应用程序中检测 Windows 11 中的 Xbox 控制器按键事件?
- python - 熊猫:填写数据框中缺少的月份
- sqlite - 我需要在 sqlite 中创建具有“一对零或一对一”关系的表。这是我到目前为止所拥有的,对吗?