简体   繁体   English

Angular NGRX/Observables 和 Reactive Forms

[英]Angular NGRX/Observables and Reactive Forms

Currently I have a project that uses NGRX for its store and reactive forms.目前我有一个项目使用 NGRX 作为其存储和反应形式。

In my application, I want to map the active item in my state to the reactive forms.在我的应用程序中,我想将我状态中的活动项映射到反应形式。 So in my component I have something like:所以在我的组件中,我有类似的东西:

export class MyComponent {

    active$: Observable<any>;

    form = this.fb.group({
        name: ['', [Validators.required]],
        description: ['', Validators.maxLength(256)]
    });

    constructor(private fb: FormBuilder, private store: Store<any>) {
        this.active$ = store.select(store => store.items);
    }
    
}

In order to avoid having to subscribe to the store and select out my items, I created a directive to bind my observable to my form:为了避免订阅商店并选择我的项目,我创建了一个指令来将我的可观察对象绑定到我的表单:

import { Directive, Input } from '@angular/core';
import { FormGroupDirective } from '@angular/forms';

@Directive({
    selector: '[connectForm]'
})
export class ConnectFormDirective {

    @Input('connectForm')
    set data(val: any) {
        if (val) {
            this.formGroupDirective.form.patchValue(val);
            this.formGroupDirective.form.markAsPristine();
        }
    }

    constructor(private formGroupDirective: FormGroupDirective) { }

}

Now in my form I simply bind it like so:现在在我的表单中,我只是像这样绑定它:

<form [formGroup]="form" novalidate (ngSubmit)="onSubmit()" [connectForm]="active$ | async">
</form>

My question is:我的问题是:

  • Is this a good idea / is there a better way to handle this?这是个好主意/有没有更好的方法来处理这个问题?

I suppose at the end of the day for complicated scenarios you are going to have to end up subscribing to the observable in the component.我想在一天结束时,对于复杂的场景,您最终将不得不订阅组件中的 observable。

You should consider splitting your components into the "Connected" and "Presentation" components.您应该考虑将组件拆分为“已连接”和“演示”组件。 Your connected component will be responsible for querying the store and emitting actions to the store, where your presentation component will create a form group and accept values from the store as @Input parameters.您的连接组件将负责查询商店并向商店发出操作,您的演示文稿组件将在其中创建一个表单组并接受来自商店的值作为 @Input 参数。 You can then listen for value changes in the form group in the presentation component and raise events with updated values to the connected component.然后,您可以侦听演示组件中表单组中的值更改,并向连接的组件引发具有更新值的事件。 Connected component will then handle the event from presentation component and raise a new action to update values in the store.然后,连接的组件将处理来自演示组件的事件并引发一个新操作来更新存储中的值。

No, this seems like a bad practice to me.不,这对我来说似乎是一种不好的做法。 Forms should be their own component used within a parent view component.表单应该是它们自己的组件,在父视图组件中使用。 You can then pass it data with the @Input decorator if it's supposed to be for edition.如果它应该用于版本,则可以使用 @Input 装饰器将数据传递给它。

So in your parent component you would do something like this:所以在你的父组件中你会做这样的事情:

<form-component [data]="active$ | async"></form-component>

And in the form component you can do this:在表单组件中,您可以执行以下操作:

ngOnInit{
    if(this.data)
        this.form.patchValue(this.data)
}

To disable a form control with ReactiveForms, you use the disable method.要使用 ReactiveForms 禁用表单控件,请使用disable方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM