K
K
Konstantin2020-10-13 12:49:17
Angular
Konstantin, 2020-10-13 12:49:17

What is the best way to accept data in a dialog box?

At the input of the dialog box I receive data:

constructor(@Inject(MAT_DIALOG_DATA) public data: { appid: number; raspprefect: Raspprefect }) {
}


Following is the form initialization:

ngOnInit() {
        this.raspprefect = Object.assign({}, this.data.raspprefect);
        this.form = this.formBuilder.group({
            docnumber: [this.data.raspprefect.docnumber, [Validators.required]],
            docdate: [null],
        });
    }


What is the best way to assign data from `public data` to form elements?

Option 1 - substitute values ​​directly:

docnumber: [this.data.raspprefect.docnumber, [Validators.required]],


In this case, the construction may fail: this.data.raspprefect.docnumber- if there is no property

2 Variant - pass the flag type = "edit" and then make the assignment:

if (data.type == 'edit') {
    this.form.get("a").setValue(this.data.raspprefect.docnumber);
}


Which also does not solve the issue with `this.data.raspprefect.docnumber`.

Option 3 - create an instance of the `raspprefect` class, in which there will be all the fields - but why - if there is an interface.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LastDragon, 2020-10-24
@Junart1

1) `docnumber: [this.data.raspprefect?.docnumber, [Validators.required]],` ( https://www.typescriptlang.org/docs/handbook/relea... )
2) `this.form. patchValue(this.data.raspprefect)` ( https://angular.io/api/forms/FormGroup#patchValue )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question