Despite setting the modal form to be bound to an instance of "Foo" during its construction, I encountered a strange issue where, post-constructor, this.foo
became undefined
. Even after verifying this through breakpoints and console.log
, the problem persisted.
--
EDIT:
Although I managed to find a workaround by moving the assignment of foo
to ngOnInit()
instead of within the constructor, my initial question regarding this behavior still remains.
--
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;
}