ionView hook doens't fire provider function in root page


In provider bellow, i get a list of user to display in home page. If i push the HomePage, the hook ionViewWillEnter works fine, but if i enter as rootPage, is returned undefined for usuarios property.

What’s going wrong?

provider ConexaoHome.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

export class ConexaoHome {

  public usuarios: any;

  constructor(public http: Http) {
    console.log('Hello ConexaoHome Provider');

  getRemoteUsers(tipo) {
    map(res => res.json()
    ).subscribe(data => {
      this.usuarios = data.results;
    return this.usuarios;



import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { ConexaoHome } from '../../providers/conexao-home';

  selector: 'page-home',
  templateUrl: 'home.html'
export class HomePage {

  public users: any;

  constructor(public navCtrl: NavController, public conexaoServico: ConexaoHome) {

 buscarUsuarios() {
    this.users = this.conexaoServico.getRemoteUsers('Pegando os usuários');
    // console.log('chamando...');
    // console.log(this.users);
    // console.log('retornando...' + this.users);

  buscar() {
    // this.teste = this.conexaoServico.getRemoteTeste('testando...');
    // console.log(this.teste);

  ionViewWillEnter() {
    console.log('ionViewDidLoad xxx Teste');
    this.users = this.conexaoServico.getRemoteUsers('Pegando os usuários');


    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>

<ion-content padding="false">
  <button ion-button (click)="buscarUsuarios()">Pegar Dados</button>
  <button ion-button (click)="buscar()">Pegar Dados 2</button>
    <button ion-item *ngFor="let user of users">
      <ion-avatar item-left>
        <img src="{{ user.picture.medium }}">
      <h2 text-wrap>{{ }} {{ }} {{ }}</h2>
      <h3 text-wrap>{{ }}</h3>


I think this callback will fire only when is the first load. You need use a callback that runs when you enter or have the page cached, like ionViewDidEnter. Take a look on NavController.


I try all the hooks, and only works if i push the HomePage and doens’t works when start a RootPage.


Maybe you have a good reason for it, but why are subscribing inside the Injectable? Wouldn’t it be much better to subscribe in the actual page where you want to retrieve the data instead of doing this: this.users = this.conexaoServico.getRemoteUsers('Pegando os usuários');

    this.users = this.conexaoServico.getRemoteUsers('Pegando os usuários');

You’re trying to log this.users, but since it’s async you never know at that moment that this.users actually contains a value. What you should do instead, is take a look at how subscribers are meant to work. Something like this inside your service:

getRemoteUsers(tipo) {
  	 	return this.http.get('').map(
  		(res: Response) => {
  			return res.json();  			

Now inside your page, subscribe to it like this:

buscarUsuarios() {
    this.conexaoServico.getRemoteUsers('Pegando os usuários').subscribe(
    	(users) => { console.log(users); this.users = users; }, 
    	(err) => { console.error(err) },
    	() => { console.log('done getting remote users') }

Now in your ionViewWillEnter call buscarUsuarios() :

ionViewWillEnter() {
    console.log('ionViewDidLoad xxx Teste');

Ionic 2 function return problem

Thank you for clearing my mistake. Now i understood the operation of subscribe.

but why are subscribing inside the Injectable?

You can tell me what is wrong? What i should read about it?