簡體   English   中英

為什么會出現TypeError:undefined不是Angular中的對象(求值”)?

[英]Why I get TypeError: undefined is not an object (evaluating '') in Angular?

我有自定義服務service.center.ts ,其中包含以下刪除方法:

deleteCenter(id: number): Observable<{}> {
  console.log(id);
  const url = `${this.D_ROOT_URL}${id}`;
  return this.http.delete(url);
}

我在模板中有按鈕:

<button type="submit" (click)="deleteCenter()">Delete</button>

list-centers.component.ts調用以下方法:

deleteCenter(): void {
  if (this.getCheckedCenters.length > 0) {
    this.getCheckedCenters.forEach(function (id) {
      id = +id;
      this.centers = this.centers.filter(c => c.id !== id);
      this.centerService.deleteCenter(id).subscribe();
    });
  }
}

this.getCheckedCenters返回ID數組,我打印了console.log並返回有效數字。

但是list-centers.component.ts的方法在控制台中顯示錯誤,並且不會在center.service.ts調用另一個方法。

錯誤:

TypeError:未定義不是對象(評估“ this.centers”)

centers: Center[];

中心數組的數據: 在此處輸入圖片說明

通過使用foreach(function ...,您將失去此作用域。您可以嘗試使用lambda foreach(p => {...

這是因為typecrypt保證了lambda中的作用域而不是功能。 您也可以使用函數和閉包

暫無
暫無

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

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