Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How split functionality in observer angular

I have some component with two methods. One of them calls authService:

onClickSubmit(data: any) {
    this.trigger = false;
    this.authService.login(data.userName, data.password);
}

drop_trigger() {
    this.trigger = false;
}

AuthService creates post resquest to backend.

login(username: string, password: string) {
      const url = this.AUTH_URL
      const headers = new HttpHeaders().set('accept', 'application/json');
      let requestBody = new FormData();
      requestBody.append('username', username);
      requestBody.append('password', password);
      const resp = this.http.post<LoginUserResponse>(
         url, requestBody, {headers}
      ).subscribe(
         data => {
            this.setSession(data);
         }
      );

This method sets session after recieve response from backend.
I need some functionality into my component (for example to call component.drop_trigger), that calls after setSession and was in component. How can i do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

best approach is to subscribe it in the component and then call the setSession after get response.

In service:

login(username: string, password: string) {
      const url = this.AUTH_URL
      const headers = new HttpHeaders().set('accept', 'application/json');
      let requestBody = new FormData();
      requestBody.append('username', username);
      requestBody.append('password', password);
      return this.http.post<LoginUserResponse>(
         url, requestBody, {headers}
      ).pipe(tap(data => this.setSession(data)));
}

Component:

onClickSubmit(data: any) {
    this.trigger = false;
    this.authService.login(data.userName, data.password)
      .subscribe(
         data => {
            this.drop_trigger();
         }
      );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading