Implementacja serwisów w angularze - Pomoc

Implementacja serwisów w angularze - Pomoc
Mateuszto
  • Rejestracja: dni
  • Ostatnio: dni
  • Lokalizacja: Poznań
  • Postów: 163
0

Hejka, napisałem prostą dość apkę: https://github.com/Mateuszto/countries/tree/master/src/app
Czy jest jakaś szansa na wykorzystanie w tym serwisów?
Dzięki za pomoc!

Kondziowsky
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 219
0

W sumie jak już stworzyłeś serwis o nazwie countries to wypada przenieść tam funkcję getCountry i w razie potrzeby załadować serwis do danego komponentu :)

W przypadku getContinent nie przenosiłbym tam, bo nazwa serwisu nie mówi nam za bardzo, że akurat kontynenty też tam znajdziemy. Chyba, że tak zakładasz, ale wtedy zmieniłbym jednak nazwę serwisu

Mateuszto
  • Rejestracja: dni
  • Ostatnio: dni
  • Lokalizacja: Poznań
  • Postów: 163
0

@Kondziowsky: @Kondziowsky:
Dałem teraz tak:
App.component

Kopiuj
 getCountry(country: string) {
    this.countriesService.getCountry(country);
  }

Serwis

Kopiuj
  allCountries: ICountries[] = Countries;

  constructor() {
  }

  getCountry(country: string) {
    if(country){
      this.allCountries = this.allCountries.filter(element => element.country.includes(country));
    } else {
      this.allCountries = Countries;
    }
  }

No i coś się wykrzaczyło :/

Kondziowsky
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 219
0

Blisko, ale jak już przerabiasz dane w serwisie to zwróć je za pomocą return :)

A jak już return będzie to w Twoim komponencie musisz przypisać gdzieś dane (stwórz sobie zmienną)

czyli np:

Kopiuj
const country = this.countriesService.getCountry(country);
Kopiuj
 getCountry(country: string) {
     let tempVar;
     country ? tempVar = this.allCountries.filter(element => element.country.includes(country)) : tempVar = Countries;
     return tempVar;
  }

lub wg Twojej wersji z ifem:

Kopiuj
  getCountry(country: string) {
    if(country){
      return this.allCountries.filter(element => element.country.includes(country));
    } else {
      return Countries;
    }
  }
Mateuszto
  • Rejestracja: dni
  • Ostatnio: dni
  • Lokalizacja: Poznań
  • Postów: 163
0

@Kondziowsky:
Końcowo

Serwis:

Kopiuj
export class CountriesService {
  allCountries: ICountries[] = Countries;

  constructor() {
  }

  getCountry(country: string) {
    if(country){
      return this.allCountries.filter(element => element.country.toLowerCase().includes(country));
    } else {
      return Countries;
    }
  }
}

Component:

Kopiuj
export class AppComponent implements OnInit {
  allCountries: ICountries[] = Countries;

  constructor(private countriesService: CountriesService) { }

  ngOnInit(): void {

  }

  getContinent(continent: string){
    this.allCountries = Countries;
    if(continent === 'World'){
      this.allCountries = Countries;
    } else {
      this.allCountries = this.allCountries.filter(country => country.continent === continent);
    }
  }

  getCountry(country: string){
    this.allCountries = this.countriesService.getCountry(country);
    console.log(this.countriesService.getCountry(country));
  }
}

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.