Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda