我正在嘗試將資料傳遞到 modal。模態是一個單獨的組件。
我可以在建構式中控制臺記錄 bsModalRef 屬性,但由于某種原因,我無法控制臺記錄 bsModalRef.content 屬性。
這是我從...啟動模式的頁面中的代碼
<button class="btn btn-primary"
type="button"
(click)="openDeleteModal(result.id)">
Edit
</button>
這是該 openDeleteModal() 方法的代碼...
public openDeleteModal(id: number): void {
this.selectedCase = this.listOfSearchResults.filter(result => result.id == id);
const initialState: ModalOptions = {
initialState: {
list: [
this.selectedCase
],
title: 'Edit Case',
whatYouWantToDelete: 'this error'
}
};
this.bsModalRef = this.modalService.show(DeleteModalComponent, initialState);
}
現在,對于模態本身的代碼,我正在像這樣匯入 BsModalRef ...
import { BsModalRef } from 'ngx-bootstrap/modal';
這是我設定的屬性...
title?: string;
whatYouWantToDelete?: string;
list?: any[];
initialState: any;
這就是我為建構式所擁有的......
constructor(
public bsModalRef: BsModalRef,
private http: HttpClient) {
this.list = this.list;
console.log("this.list: ", this.list);
console.log("this.bsModalRef in constructor: ", this.bsModalRef);
console.log("this.bsModalRef.content in constructor: ", this.bsModalRef.content);
}
這是我在 console.log 中看到的螢屏截圖...

這是 BsModalRef 物件的內容部分......

我的問題是,如何從建構式訪問串列屬性中的資料?該物件具有我需要填充模態中的表單的屬性。
說法不一...
這行代碼...
this.bsModalRef = this.modalService.show(DeleteModalComponent, initialState);
打開模態并將資料作為初始狀態傳入。如何從模態本身訪問通過 initialState 物件傳入的資料?
uj5u.com熱心網友回復:
這里有 2 個關鍵點:
- Javascript 控制臺中記錄的物件是“活動的”,因此展開它們會顯示物件的當前內容。當您第一次呼叫 console.log() 時,Content.list 是空的。然后資料被更新,因此也在控制臺上。查看問題
為了查看真實資料,我更喜歡這個:
console.log(JSON.parse(JSON.stringify(this.bsModalRef)));
- 似乎 bsModalRef 有問題。您可以通過以下方式訪問內容:
setTimeout(() => { console.log(this.bsModalRef.content), 0});
但這是一種解決方法。您應該將此報告給 ngx-bootstrap存盤庫。
uj5u.com熱心網友回復:
我的問題是,如何從建構式訪問串列屬性中的資料?該物件具有我需要填充模態中的表單的屬性。
訪問資料的正確方法是在ngOnInit生命周期掛鉤方法中。
title,whatYouWantToDelete并且list資料將存在于 ngOnInit 中;您不需要通過 bsModalRef 實體訪問它。試試這個:
ngOnInit() {
console.log(this.list);
console.log(this.whatYouWantToDelete);
console.log(this.title);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408145.html
標籤:
