0

I'm creating form which is using data of the current logged in user. Function which is setting value of form is done before getting user data.

ngOnInit() {
    this.auth.getUserState()
      .subscribe( user => {
        this.user = user;
      });
    this.resetForm();
  }

  resetForm(form?: NgForm) {
    if (form != null) { form.resetForm(); }
    this.service.formData = {
      who: this.user.email,
      id: null,
      name: '',
      content: '',
      date: null,
      done: false
    };
  }

How can i stop initialization of "resetForm" before component get current user data and complete "who" field with properly value?

1 Answers1

2

You can place it inside the subscription of getUserState():

ngOnInit() {
    this.auth.getUserState()
      .subscribe( user => {
        this.user = user;
        this.resetForm();
      });      
  }

Thereby, once the user information is present then only the resetForm(...) is called.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57