出现Angular列表不显示数据的问题,可能有多个原因。以下是一些可能的解决方法:
- {{ item }}
确保items是在组件中定义并正确赋值。
import { Component, OnInit } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css']
})
export class ListComponent implements OnInit {
items: any[];
constructor(private dataService: DataService) { }
ngOnInit() {
this.dataService.getItems().subscribe(data => {
this.items = data;
});
}
}
- 0" *ngFor="let item of items">{{ item }}
- No items found.
可以使用ChangeDetectorRef的detectChanges方法来手动触发变化检测。
import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { DataService } from './data.service';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ListComponent implements OnInit {
items: any[];
constructor(private dataService: DataService, private cdr: ChangeDetectorRef) { }
ngOnInit() {
this.dataService.getItems().subscribe(data => {
this.items = data;
this.cdr.detectChanges();
});
}
}
这些方法可能会解决Angular列表不显示数据的问题。根据具体情况,可能需要组合使用多种方法来解决问题。如果问题仍然存在,可能需要进一步检查代码和调试。