1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

how to pass a http response from component to another in angular

Discussão em 'Angular' iniciado por user21681228, Setembro 30, 2024.

  1. user21681228

    user21681228 Guest

    i cannot update the data

    import { Injectable } from '@angular/core';
    import { BehaviorSubject, Observable } from 'rxjs';
    @Injectable({
    providedIn: 'root'
    })
    export class DataService {
    private data1 = new BehaviorSubject<any>(null);
    data = this.data1.asObservable();
    data3:any;
    constructor() { }
    sendData(data: any) {
    this.data1.next(data);
    }
    getData(): Observable<any> {
    return this.data;
    }
    }


    app.component.ts file:

    public postData(name: string) {
    const headers = { "Content-Type": "multipart/form-data" };
    this.http.post("http://localhost:5000/app/", name, { headers: headers })
    .subscribe(
    (response) => {
    console.log(response);
    this.data = response;
    },
    (error) => {
    console.error(error);
    });
    }


    parcourir.component.ts file:

    export class ParcourirComponent implements OnInit{
    data1:any;
    constructor(private service:DataService){}
    ngOnInit() {
    this.service.getData().subscribe(response=>{
    this.data1 = response;
    console.log(this.data1)
    });
    }


    It logs null to the console, it's not updating data i want the response get in the http request in app.component.ts to be sent to parcourir.component.ts.

    Continue reading...

Compartilhe esta Página