As you can see in the code, i tried to dynamically create array values inside an object, it doesn’t have a problem when debugging, but when i tried to use push method in the array it throws an error.I am working on Ionic 3 with angular 4.I am calling the function on input box’s keyup.enter event.
// this is the object
private newsFeeds=[
{
avatarUrl:’…/assets/fb/avatar.png’,
userName:‘Karim Benzema’,
time:‘16 minutes ago’,
caption:‘Wow!!!’,
imageUrl:‘https://upload.wikimedia.org/wikipedia/commons/e/ec/Ara_ararauna_Luc_Viatour.jpg’,
like:‘Like’,
numberOfLikes:2,
comments:[‘hello’],
isCommentEnable:false
},
{
avatarUrl:’…/assets/fb/avatar2.png’,
userName:‘Unknown Singh’,
time:‘2 hrs ago’,
caption:‘Pc:Unknown’,
imageUrl:‘https://i.pinimg.com/736x/50/e0/b6/50e0b65efd2d634053d7a8d1cd9d94fc--so-funny-funny-
stuff.jpg’,
like:‘Like’,
numberOfLikes:3,
comments:[],
isCommentEnable:false
}
];
postComment(index,value){
console.log(${index} : ${value}
); //shows correct index and value
this.newsFeeds[index].comments.push(value); //–>error
//Throws runtime error ‘comments.push is not a function’
}
my HTML code:
<ion-list [virtualScroll]= “newsFeeds”>
<ion-card *virtualItem=“let f; let i=index”>
<span *ngIf=“f.isCommentEnable”>
<input type=“text” [(ngModel)]=“f.comments” (keyup.enter)=“postComment(this.i,item.value);” #item>