簡體   English   中英

Angular材料數據表不顯示服務數據

[英]Angular material data table not displaying service data

我試圖在角度材料數據表中顯示一些數據,我的服務類方法有數據但是它沒有顯示在數據表中,請幫助我,從最近3小時內無法在數據表中找到錯誤

服務類和api



getfriendAnswers(id: number): Observable<FriendDetails[]> {
      debugger;
      return this.httpClient.get<FriendDetails[]>(this.apiUrl + '/Questions/yourFriendAnswerdCount/' + id)

   }


[HttpGet, Route("yourFriendAnswerdCount/{id}")]
   public ActionResult getFriendAnswerdCount(int id)
 {
   return Ok(userRepository.getFriendAnswerdCount(id));
  }


朋友詳情課

export interface FriendDetails {
    Id: number;
    UserId: number;
    AnsweredCount: number;
    FriendName: string;
}

組件類

export class FriendAnswredComponent implements OnInit {
id;
  constructor(private service: UserService,private route: ActivatedRoute) { }

  dataSource :FriendDetails[]=[];

  displayedColumns: string[] = [ 'FriendName', 'AnsweredCount',];

  ngOnInit() {
    this.id = parseInt(this.route.snapshot.paramMap.get('id'));


    this.service.getfriendAnswers(this.id).subscribe(b => {
      debugger;
      this.dataSource = b;

    });
  }

}

材料數據表模板

<mat-card>
  <mat-card-title>your friends answered count questions are</mat-card-title>
  <mat-card-content>

<div *ngIf="dataSource">
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">

<!--- Note that these columns can be defined in any order.
      The actual rendered columns are set as a property on the row definition" -->


<!-- Name Column -->
<ng-container matColumnDef="FriendName">
  <th mat-header-cell *matHeaderCellDef> FriendName </th>
  <td mat-cell *matCellDef="let element"> {{element.FriendName}} </td>
</ng-container>

<!-- Weight Column -->
<ng-container matColumnDef="AnsweredCount">
  <th mat-header-cell *matHeaderCellDef> AnsweredCount </th>
  <td mat-cell *matCellDef="let element"> {{element.AnsweredCount}} </td>
</ng-container>


<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
</mat-card-content>
</mat-card>
{{dataSource[0]?.AnsweredCount}}

service返回此輸出
{id: 5, userId: 11, answeredCount: 2, friendName: "sarala"}我得到空數據表甚至服務返回數據

你正試圖綁定dataSource[0]?.AnsweredCount當你的對象中不存在AnsweredCount

它應該是一個 nsweredCount,而不是A nsweredCount:

{{dataSource[0]?.answeredCount}}

暫無
暫無

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

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