1

I have a component which is used for route navigation without component reload:

export class TableComponent extends SafeComponent implements OnInit {
  customersMe$: Observable<ICompanyCustomer>;
  customersMe: ICompanyCustomer;
  tables$: Observable<ITable[]>;
  headersLoaded$: Observable<boolean>;
  headerLoading = true;

  table: ITable;
  TableForm: FormGroup;
  constructor(
    private store: Store<State>,
    private router: Router,
    private route:ActivatedRoute,
    private changeDetectorRef: ChangeDetectorRef,
  ) {
    super();
  }

  ngOnInit(): void {
    this.tables$ = this.store.pipe(select(getTables));
    this.headersLoaded$ = this.store.pipe(select(getLoaded));

    combineLatest([this.tables$, this.headersLoaded$, this.route.params])
      .subscribe(([tables, loaded, params]) => {
        if (loaded) {
          this.headerLoading = false;
          const table = tables.find(x => x.id === Number(params['id']));
          if (table) {
            this.table = table;
          } else {
            // navigate 404
          }
          this.changeDetectorRef.detectChanges();
        }
      })


  /*  this.tableForm = new FormGroup({
   *    start: new FormControl(this.table.time_start),
   *    end: new FormControl(this.table.time_finish)
   *});*/

  }
}

As you notice here I subscribe to route and update data on new id in route path. How can I update data in Form Group? Thank you!

1
  • hi if you just wanna know how to update a form take a look about patchValue ? exemple : this.form.patchValue({ name: 'Todd Motto', event: { title: 'AngularCamp 2016', location: 'Barcelona, Spain' } }); Commented Sep 15, 2020 at 9:16

2 Answers 2

2

In Angular If you want to reload the form. then you need to do like this,

this.form.reset();

& when you are on a new route then you can do this.

this.form.patchValue({name: '---', age:'---'});
Sign up to request clarification or add additional context in comments.

Comments

1

If you are trying to populate the form fields with values, you could try: For populating all fields:

this.form.setValue({name: 'Kakashi', address: { village: 'Konoha', country: 'Fire'}});

For populating some fields:

this.form.patchValue({name: 'Kakashi', address: { village: 'Konoha', country: 'Fire'}});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.