如何解决Ngrx Component-store :更新商店中的特定字段
我在 NGRX 的组件存储库中寻找更新我的模型的单个字段。
这里是商店
export interface MyModelState {
model: MyModel;
}
@Injectable({
providedIn: 'root'
})
export class MyModelStoreService extends ComponentStore<MyModelState> {
constructor() {
// the initial state could contain an empty model
super({ model: null });
}
readonly model$: Observable<MyModel> = this.select(state => state.model);
readonly updateModel = this.updater( (state,newModel: MyModel) => ({ ...state,model: newModel }) );
}
以及更新模型中值的组件函数:
updateModel(key,value) {
this.MyModelStoreService.patchState((state) => { ...state,model: { ...state,key: value } });
}
查看
<div *ngIf="myModel$ | async as myModel">
<div *ngIf="myModel.constructor.name==='First'">
<input type="number" [(ngModel)]="myModel.value" (ngModelChange)="updateModel('key',$event)" /><br />
</div>
<div *ngIf="myModel.constructor.name==='Second'">
<!-- same for other models type -->
</div>
</div>
我知道这个例子不能工作(updateModel 方法),但我可以用正确的方式做到这一点?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。