How to receive data with parameter from Angular service

Discussão em 'Angular' iniciado por Alex Al, Abril 21, 2025.

  1. Alex Al

    Alex Al Guest

    How to receive data from the service with parameters. I have 2 components and service. From one component I must to receive the data in the other component via the service. Look me code header.component.html

    <li><a routerLink="contact-us">
    <select (change)="onChange($event.target.value)">
    <option>CONTACT US</option>
    <option *ngFor="let coun of countriesShow">{{ coun }} </option>
    </select>
    </a>
    </li>


    header.component.ts

    onChange(data: string){
    this.countrySelect = this.ctry.onChange(data);
    return this.countrySelect;
    }


    selectedcountry.service.ts

    public countrySelect: any;

    onChange(selectedCountry: string) {
    this.countrySelect = selectedCountry;
    return this.countrySelect;
    }

    getCountry(){
    return this.countrySelect;
    }


    contact-form.component.ts (this component must to recieve data from header and service)

    public countrySelect: any;
    constructor(private country: SelectedcountryService) { }


    ngOnInit() {
    this.countrySelect = this.country.getCountry();
    console.log(this.countrySelect) // undefined
    }

    Continue reading...

Compartilhe esta Página