Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

268
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda