我的计划是在我的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;' –