Issue
It's a simple alertController box, which create a checklist element. When I click on add, the function addCheck() of the class ChecksPage is called :
export class ChecksPage implements OnInit {
checkList: Check[] = [];
constructor(private alertCtrl: AlertController) { }
ngOnInit() {
}
addCheck(){
this.alertCtrl.create({
header: 'Nouvelle liste',
inputs:[{
name:'nom',
placeholder: 'Nom'
}],
buttons:[
{
text: 'Annuler',
role: 'cancel'
},
{
text: 'Ajouter',
handler: data => {
if(typeof data.nom !=null){
let newCheck: Check;
newCheck.id = ''+this.checkList.length;
newCheck.title = data.nom;
this.checkList.push(newCheck);
}
}
}]
}).then(alertEl => alertEl.present());
}
}
But, I have an error : the variable newCheck is undefined. Why ? Check is an interface :
export interface Check{
id: string;
title: string;
}
Solution
You didn't init your variable, please do as follow:
const newCheck = {
id: ''+this.checkList.length,
title: data.nom
} as Check;
Answered By - Ernesto Schiavo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.