我的計劃是在我的ngrx商店中存儲表單的值,以允許我的用戶在網站周圍導航並返回到表單,如果他們希望。這個想法是,表單的值將使用可觀察值從商店重新填充。如何使用Observable初始化Reactive Angular2表單?
這裏是如何我目前做:
constructor(private store: Store<AppState>, private fb: FormBuilder) {
this.images = images;
this.recipe$ = store.select(recipeBuilderSelector);
this.recipe$.subscribe(recipe => this.recipe = recipe); // console.log() => undefined
this.recipeForm = fb.group({
foodName: [this.recipe.name], // also tried with an OR: (this.recipe.name || '')
description: [this.recipe.description]
})
}
這家店給我看到通過我的選擇正常工作經過一個初始值,而是由我的時間創建窗體,我不我認爲這個價值已經回來了。因此this.recipe
仍未定義。
這是錯誤的方法,或者我可以以某種方式確保在創建表單之前返回observable?
我無法通過這種方式得到它。 '類型Observable'上不存在startsWith()。該函數似乎只存在於字符串中。 –
對不起,該方法應該是'startWith'。 https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/startwith。md – shusson
'.startWith()'在第一次迭代中工作,但是當第二次(dev模式)運行時,它再次未定義。我通過刪除'startWith()'並將選擇器更改爲:'return _.cloneDeep(state.recipebuilder)|| someDefaultValue;' –