我有一些動態生成的窗體並向它們傳遞值。我覺得有人必須解決這個問題,否則我錯過了一些顯而易見的事情,但我找不到任何提及。角動態窗體可觀察屬性綁定
因此,例如,我有三個組成部分,父母,孩子,然後這個孩子的孩子。對於名稱,我會去,formComponent,questionComponent,textBoxComponent。這兩個孩子都在使用changeDetection.OnPush。
所以形式部件通過輸入經過一些值下降到questionComponent,有的使用異步管訂閱在商店它們各自的值。
QuestionComponent動態創建不同的組件,然後將它們放置在頁面上,如果他們匹配(這麼多類型的組件,但每個questionComponent只處理一個組件上
一些代碼:
@Input() normalValue
@Input() asyncPipedValue
@ViewChild('questionRef', {read: ViewContainerRef}) public questionRef: any;
private textBoxComponent: ComponentFactory<TextBoxComponent>;
ngOnInit() {
let component =
this.questionRef.createComponent(this.checkboxComponent);
component.instance.normalValue = this.normalValue;
component.instance. asyncPipedValue = this. asyncPipedValue;
}
這適用於normalValues的所有實例,但不適用於asyncValues。我可以在questionComponent的ngOnChanges中確認值正在更新,但該值不會傳遞給textBoxComponent。
什麼我基本上需要的是異步管道,但不適用於模板。我已經嘗試了多種解決方案來傳遞asyncValues,我試着檢測asyncPipeValue何時發生變化,並在textBoxComponent上觸發changeDetectionRef.markForChanges(),但只有當我將changeDetectionStrategy更改爲正常時,纔會起作用,這會影響性能我從使用ngrx獲得收益。
這似乎是太大的監督,尚未有解決的辦法的,所以我假定這只是我沒有想到的東西。有什麼想法嗎?
你可以提供重擊嗎? – yurzui
你看過BehaviorSubject嗎?您可以讓服務運行邏輯並/或將您的asyncValue修改爲BehaviorSubject,並讓每個組件訂閱該BehaviorSubject的Observable。 –
^爲什麼我們在使用ngrx時需要'BehaviorSubject',我們可以直接存儲在'store'中? – Skeptor