If you have a parent component with an object, and you use a selector to display a child component, you can pass the object to the child component this way in Angular 2:
In the parent.html
<child-component [nameOfObjectInsideChildComponent] = “nameOfObjectInsideParentComponent”>
Then in child.ts @Input nameOfObjectInsideChildComponent: any;
But in Ionic 2, I’m having problems getting the object in the child component. I get no errors but my object is undefined. Maybe in Ionic this is done in a different way?
I can’t even console.log (nameOfObjectInsideChildComponent) on the constructor. I don’t have an html code on my child component yet because I don’t have a value in my object. I don’t think it’s going to be useful anyways.
That’s correct. But what I don’t understand is that .html is just an interpretation of what we want to show from our .ts. I just saw that the parent .html doesn’t show anything, sorry it’s here:
@Input () brand: string; constructor(public navCtrl: NavController) { console.log (this.brand); //I get a value of undefined here. Shouldn't it be the brand.key passed by the parent component? }
child.html <ion-content padding> <ion-item *ngFor = "let car of cars"> <div>{{car.model}}</div> </ion-item> </ion-content>