I have a modal consisting of a form that is bound to an instance of "Foo." During construction of the modal, foo is set correctly. I double-checked both using breakpoints and console.log. However, at any time after the constructor, this.foo is undefined.
--
EDIT:
I have found a solution, but my question still stands. My solution was to move my assignment of foo to ngOnInit() instead of the constructor.
--
Template:
<ion-header>
<ion-toolbar>
<ion-title>New Foo</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item>
<ion-input placeholder="Name" [(ngModel)]="foo.name"></ion-input>
</ion-item>
<!-- etc... -->
</ion-list>
<ion-button expand="block" color="primary" (click)="create()">
Create
</ion-button>
</ion-content>
Component:
export class FooModalComponent implements OnInit {
foo: Foo = new Foo();
constructor(
private modalController: ModalController,
private navParams: NavParams
) {
const inputFoo = this.navParams.get("foo");
if (inputFoo) {
// this shouldn't matter, as foo would have to be truthy (not undefined)
this.foo = inputFoo;
}
// logs this.foo as expected
console.log(this.foo);
}
ngOnInit() {
// logs: undefined
console.log(this.foo);
}
close() {
this.modalController.dismiss();
}
create() {
this.modalController.dismiss(this.foo);
}
}
Model:
export class Foo {
name: string;
}