NavCtrl push


#1

Hi,

I’m newbie in ionic 2 how to push navCtrl when the second page has multiple component.

Here’s my constructor code in second page:

constructor(public navCtrl: NavController, private contacts: Contacts) {
}

addPerson(){
let contact: Contact = this.contacts.create();

contact.name = new ContactName(null, 'Smith', 'John');
contact.phoneNumbers = [new ContactField('mobile', '6471234567')];
contact.save().then(
  () => alert("save"),//console.log('Contact saved!', contact),
  (error: any) => console.error('Error saving contact.', error)
);

}

and here’s is my code in first page:
goToAddPerson() {
this.navCtrl.push(AddContact);
}

When I delete private contacts: Contacts in constructor on second page, the navCtrl.push work fine, but the problem is i cant use contacts component.

Thank you in advance .


#2

You speak of “contacts component”, but the way you’re injecting Contacts makes it look more like a service. Which is it, and what does “can`t use” mean?


#3

Hi rapropros,

Thank you for your response .This code not working when I remove contacts in constructor.

addPerson(){
let contact: Contact = this.contacts.create();

contact.name = new ContactName(null, ‘Smith’, ‘John’);
contact.phoneNumbers = [new ContactField(‘mobile’, ‘6471234567’)];
contact.save().then(
() => alert(“save”),//console.log(‘Contact saved!’, contact),
(error: any) => console.error(‘Error saving contact.’, error)
);
}


#4

Apologies if I’m explaining things you already know.

When you provide access control qualifiers (private/public) on constructor parameters, TypeScript automatically does two things: declare an object property with that name, and initialize it with whatever DI has provided for it at construction time. So if you remove private contacts: Contacts from the constructor, it will cease to do those two things, and this.contacts will not exist when you try to use it.


#5

Hi rapropos,

Yes I already tried,the command are not working . Can you provide me example how can i solve this problem.

Thank you


#6

Frankly, no, because you have not described what the problem actually is.


#8

Hi

Problem solve, rapropos thank you for your explanation for this.

constructor(public navCtrl: NavController,public navParams: NavParams) {
this.contacts = navParams.get(‘contact’);
}