How to set default form Value from Observable?

Leonard Loss

Here is my ts component

export interface Product{
    id?:string,
    name:string,
    price:string;
    quantity:string;
    tags:Tags[];
    description:string;
    files: File[];
}

product$:Observable<Product | undefined>;

ngOnInit(): void { 
  this.store.dispatch(new productActions.LoadProduct(fromProduct.getCurrentProductId.toString()));
  this.product$ = this.store.pipe(select(fromProduct.getCurrentProduct));
}

Last and the second statements gather the value of product observable and work fine.

this.product = this.fb.group({
    name:['',[
      Validators.required,
      Validators.minLength(1),
      Validators.maxLength(128)
    ]],
    price:['',
    Validators.required],
    tags:this.fb.array(this.tags),
    quantity:['',Validators.required],
    description:['',
    Validators.required]
  });
}

Now what I want is to set the default value of Form from Product$ (observable)

The above code sets the default value of name ''. But I want to set a default value from (product$ | async).name----->> this works fine in Html, but I have no idea how to do it in typescript.

Thanks for helping me.

maiksch

You could put your whole FormGroup initialization into a subscribe. Don't forget to use the first operator though, since you only want to do this once and then unsubscribe.

this.product$
    .pipe(first())
    .subscribe(product => {
        this.product = this.fb.group({
            name:[product.name,[
              Validators.required,
              Validators.minLength(1),
              Validators.maxLength(128)
            ]],
            price:['',
            Validators.required],
            tags:this.fb.array(this.tags),
            quantity:['',Validators.required],
            description:['',
            Validators.required]
          });
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

ember-cli How to set default value from form select?

How to set default value in Eureka-Form

How to set default value in laravel collective form

Django - How Do I Set A Default Value In A Form To Be The Current User?

How to set default value to None for Django choices form field

How to set a default selected value in a form select in Kendo UI?

how to set default value for select field inside a form

How do I set the default form control value in this situation?

How to set default value for mat-select with reactive form is used

How to set default value in dropdown of admin grid form in magento?

How to set default selected value for form fields in Magento?

How to set default value about RxJS in Angular form

How to set a value from observable to a variable in Angular 2

How to set a value on a filtered Observable

RxJS: Setting default value for observable from another observable

Set form's combobox default value from first table value, Access

set a default value in user form django

Resetting form and set default value Angular 2

Unable to set Form field value by default in angularjs

Redux Form - Checkbox with a default value and set it to be disabled

Set default value to Form:text() in Laravel

Set form exposed filter default value

How to set a default selection and value in dropdownlist with data from database?

From where this date picker is included and how to set default value

How to set default field value from xml code in odoo?

Android how to set default value of EditTextPreference from SharedPreference?

How to set a route parameter default value from request url in laravel

How to set default value from mysql join interval yearmonth

C# - How to set value to default from table in combobox datagridview?

TOP Ranking

HotTag

Archive