Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Ionic usando una condición para comparar una cadena con los datos devueltos en un Observable

En este código recupero datos sobre un país como observable. Luego trato de comparar mi cadena this.city con this.capital que recuperé del Observable. Si los dos no son iguales, quiero mostrar un nuevo párrafo en el html cambiando el booleano oculto a falso. Sé que this.city y el observable this.capital no son iguales, pero no muestra el párrafo en el html después de llamar a showHeader(). Me pregunto si puede comparar datos observables con cadenas de esta manera.

 import { Component } from '@angular/core'; import { NavController } from 'ionic-angular'; import { SettingsPage } from '../../pages/settings/settings'; import { Storage } from '@ionic/storage'; import { CityDataProvider } from '../../providers/city-data/city-data'; @Component({ selector: 'page-home', templateUrl: 'home.html' }) export class HomePage { hidden: boolean = true; hiddenTwo: boolean = true; city: string; cityData: any[]; capital: string; cityLowerCase: string; constructor(public navCtrl: NavController, private storage: Storage, private cdp: CityDataProvider) { } async ionViewWillEnter() { const data = await this.storage.get("city") .then((value) => { if (value == null) { this.hidden = false; } else if (value !== null) { this.hidden = true; } this.city = value; }) .catch((error) => { alert("Error accessing storage.") }) this.cdp.getCityData(this.city).subscribe(data => { this.cityData = data; this.capital = data[0].capital.toString().toLowerCase(); this.cityLowerCase = this.city.toLowerCase(); this.showHeader(this.cityLowerCase, this.capital); }); } showHeader(a: string, b: string) { if (a != b){ this.hiddenTwo = false; } } openSettingsPage() { this.navCtrl.push(SettingsPage); };`enter code here` }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como está usando then this.storage.get("city") , this.city aún no se ha configurado cuando llama a this.cdp.getCityData(this.city) . Solo usa await correctamente.

Además, algunos consejos básicos:

  • if(a == b){...} else if(a != b){...} es esencialmente solo if(a == b){...}else{...}
  • if(condition){value = true}else{value = false} es esencialmente solo value = condition

 async ionViewWillEnter() { try { this.city = await this.storage.get("city"); this.hidden = this.city == null; } catch (error) { alert("Error accessing storage.") } this.cdp.getCityData(this.city).subscribe(data => { this.cityData = data; this.capital = data[0].capital.toString().toLowerCase(); this.cityLowerCase = this.city.toLowerCase(); this.showHeader(this.cityLowerCase, this.capital); }); } showHeader(a: string, b: string) { this.hiddenTwo = a != b; }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!