簡體   English   中英

角 | NgFor 顯示數組內容

[英]Angular | NgFor to display the array contents

我是 Angular 的新手並試圖獲取postdata 中的值,但是當我嘗試對其進行迭代時,它只給了我第一個值。 附上我的代碼:

 posts;

 constructor(private getpostservice:GetpostsService) { }

 ngOnInit(): void {
  this.getpostservice.getposts().then(data=>{
  this.posts = [data];

  console.log("Inside home",this.posts);
 })
}

這是我的 html 邏輯:

<tr *ngFor="let p of posts;index as i">


        <td>
          {{p.postdata[i].title}}
        </td>

      </tr>

這是數據格式 .

有人可以幫我提取標題和正文嗎?

我認為您試圖根據控制台數據實現的目標是:

 constructor(private getpostservice:GetpostsService) { }

 ngOnInit(): void {
  this.getpostservice.getposts().then(data => {
    this.posts = data.postdata;
    console.log("Inside home",this.posts);
 });

<ng-container *ngIf="posts">
  <tr *ngFor="let p of posts">
    <td>
      {{p.title}}
    </td>
  </tr>
</ng-container>

也許您還應該考慮使用Observables而不是Promises ,您需要習慣它,但它更強大且更易於使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM