Pass data from father to son ionic2

1

Hi, I want to pass data from a parent component to a son on ionic2, I know it's done with input, but the problem is that the data is sent to me empty, using a normal angular2 application, it sends me the data without problems, but using ionic, I do not know what happens.

tabs.ts (The son)

import { Component,Input,OnInit,Directive} from '@angular/core';


@Component({
  templateUrl:'tabs.html',
  selector:'tabs-root',

})
export class TabsPage implements OnInit{
    @Input() listaPaginas:Array<any>;
    @Input() segundo:string;
  // this tells the tabs component which Pages
  // should be each tab's root Page

  constructor() {

  }
  ngOnInit(){

    for(let i=0;i<this.listaPaginas.length;i++){
        console.log(this.listaPaginas[i]);
    }
  }
}

transactions.ts Father

............. ..............

@Component({
  selector: 'page-transacciones-examples',
  templateUrl: 'transacciones-examples.html',

})

export class TransaccionesExamplesPage {
  transactions:any;
  latitud:any=null;
  user:string="juanito11";
  cargando:Boolean=false;
  longitud:any=null;
  addingPage:Component=AddingPage;
  listaPaginas:Array<any>=[];
  constructor(public navCtrl: NavController, public navParams: NavParams) {}

  ionViewWillEnter() {
    this.cargando=true;
    this.loadTransactions();

    this.listaPaginas.push(
      {component:HomePage,title:"Home",icon:"home"},
      {component:AboutPage,title:"About",icon:"information-circle"},
      {component:ContactPage,title:"Contacts",icon:"contacts"}      
      )

  }

  loadTransactions(){
    setTimeout(()=>{
      Transaction.all()
      .then((os)=>{
        this.transactions=os;
      });
      this.cargando=false;
    },3000);

  }
}

the html where I call the son

<tabs-root [listaPaginas]="listaPaginas" [segundo]="user"></tabs-root>

I would appreciate your help.

    
asked by Kevin AB 06.02.2017 в 10:43
source

2 answers

1

If I'm not mistaken you need the reference to the model in the parent in your case it should be user and on the label use [ngModel]="segundo" or [(ngModel)]="segundo" :

import { Component,Input,OnInit,Directive} from '@angular/core';
@Component({
   templateUrl:'tabs.html',
   selector:'tabs-root'
})
 export class TabsPage implements OnInit{
      @Input() listaPaginas:Array<any>;
      @Input('user') segundo:string;
      // this tells the tabs component which Pages
      // should be each tab's root Page
     constructor() {}
     ngOnInit(){
      for(let i=0;i<this.listaPaginas.length;i++){
      console.log(this.listaPaginas[i]);
    }
  }
 }
    
answered by 19.02.2017 в 18:46
0

It is possible that the ngOnInit is running before the Parent sends the data to the child , so in console it seems that data is not arriving, that can be add to that in the view of Son there is an error.

    
answered by 28.03.2018 в 00:25