4 回答

TA貢獻1906條經驗 獲得超3個贊
您還可以嘗試通過強制值引用更新來強制更改檢測,例如通過擴展運算符:
this.endpointService.loadData().subscribe((response) => { this.data = [...response]; });

TA貢獻1847條經驗 獲得超11個贊
我已經解決了這個問題,更新了子輸入傳遞的屬性,而不是重新分配它。我認為 Angular 將輸入作為副本傳遞。所以,在訂閱正文中你應該使用
resultOfSubscribe.forEach((v) => varPassedByInput.push(v));

TA貢獻1906條經驗 獲得超10個贊
我用靜態字符串數組替換了該服務,效果很好。我認為可觀察訂閱有問題。
子組件.ts
import { Component, OnInit,Input } from '@angular/core';
@Component({
selector: 'child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
@Input() data: any[] = [];
constructor() { }
ngOnInit() {
}
}
父組件.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'parent',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css'],
})
export class ParentComponent implements OnInit {
data: string[] = [];
constructor() {}
ngOnInit() {
this.data = ['hello', 'world','aaa','ddd'];
}
}

TA貢獻1811條經驗 獲得超4個贊
模板表達式中似乎存在一些其他錯誤,導致整個模板失敗。這是我創建的 stackblitz,一切正常:https://stackblitz.com/edit/angular-ivy-w2ptbb ?file=src%2Fapp%2Fhello.component.ts
您在控制臺中可能有一些錯誤嗎?
添加回答
舉報