如何将对象放入另一个对象 Angular / Javascript
How to put object into another object Angular / Javascript
我正在制作一个应用程序,但在尝试将一个数组推入另一个数组时卡住了。
我得到一个错误 "Cannot read property 'push' of undefined."
这是对象:
export var CARDS: any[] = [
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "John Appleseed",
avatar: "url"
},
{
content: "I like this card!",
user: "John Doe",
avatar: "url"
}
]
},
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "Jane Doe",
avatar: "url"
}
]
}];
我的应用程序中有一个选项可以向卡片添加评论。因此我创建了:
newComment = {
content: "",
user: "",
avatar: ""
};
然后使用一个按钮调用 submitComment 函数:
submitComment(comment) {
console.log(this.newComment);
this.comment.push(newComment);
}
我的 HTML 看起来像这样(仅供参考:我正在使用离子,这解释了离子输入)
<ion-input type="text" placeholder="Type a comment..." [(ngModel)]="newComment.content"></ion-input>
<button (click)="submitComment(comment)">Send comment</button>
我在我的 submitComment 函数中做了一个 console.log,它显示了正确的信息。但是推动不起作用。我收到错误“无法读取未定义的 属性 'push'”。我在括号之间尝试了 "this.newComment",但它也不起作用。
我才刚刚开始了解 Angular/JS,所以这可能是一些我无法理解的简单问题。预先感谢您的帮助:-)
编辑:我忘了添加。我执行 *ngFor="let comment of card.comments" 来显示所有评论。因此我只是在 HTML.
中使用了 "comment"
您的问题是因为您提供给 submitComment
方法的 comment
变量为空。所以你不能在上面调用任何方法(特别是push
)...
否则您的 CARDS
属性 似乎没有正确定义。您可以这样定义它:
CARDS: any [] = [
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "John Appleseed",
avatar: "url"
},
{
content: "I like this card!",
user: "John Doe",
avatar: "url"
}
]
},
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "Jane Doe",
avatar: "url"
}
]
}
];
感谢蒂埃里,我解决了我的问题。 :-)
我查看了代码中的一些小东西。这是解决方案,适用于遇到同样问题的每个人。
我的问题出在我用点击功能给出的值内。我第一次
(click)="submitComment(comment)"
但 'comment' 未定义。我以为我的 ngFor="let comment of card.comments" 会定义它,但我的点击功能在 ngFor 之外。因此我不得不创建
(click)="submitComment(card)"
在我的打字稿文件中如下:
submitComment(card) {
console.log(this.newComment);
card.comments.push(this.newComment);
}
我正在制作一个应用程序,但在尝试将一个数组推入另一个数组时卡住了。 我得到一个错误 "Cannot read property 'push' of undefined."
这是对象:
export var CARDS: any[] = [
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "John Appleseed",
avatar: "url"
},
{
content: "I like this card!",
user: "John Doe",
avatar: "url"
}
]
},
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "Jane Doe",
avatar: "url"
}
]
}];
我的应用程序中有一个选项可以向卡片添加评论。因此我创建了:
newComment = {
content: "",
user: "",
avatar: ""
};
然后使用一个按钮调用 submitComment 函数:
submitComment(comment) {
console.log(this.newComment);
this.comment.push(newComment);
}
我的 HTML 看起来像这样(仅供参考:我正在使用离子,这解释了离子输入)
<ion-input type="text" placeholder="Type a comment..." [(ngModel)]="newComment.content"></ion-input>
<button (click)="submitComment(comment)">Send comment</button>
我在我的 submitComment 函数中做了一个 console.log,它显示了正确的信息。但是推动不起作用。我收到错误“无法读取未定义的 属性 'push'”。我在括号之间尝试了 "this.newComment",但它也不起作用。
我才刚刚开始了解 Angular/JS,所以这可能是一些我无法理解的简单问题。预先感谢您的帮助:-)
编辑:我忘了添加。我执行 *ngFor="let comment of card.comments" 来显示所有评论。因此我只是在 HTML.
中使用了 "comment"您的问题是因为您提供给 submitComment
方法的 comment
变量为空。所以你不能在上面调用任何方法(特别是push
)...
否则您的 CARDS
属性 似乎没有正确定义。您可以这样定义它:
CARDS: any [] = [
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "John Appleseed",
avatar: "url"
},
{
content: "I like this card!",
user: "John Doe",
avatar: "url"
}
]
},
{
channel: "facebook"
date: "2016-01-07"
comments: [
{
content: "Hey would you look at this card?",
user: "Jane Doe",
avatar: "url"
}
]
}
];
感谢蒂埃里,我解决了我的问题。 :-) 我查看了代码中的一些小东西。这是解决方案,适用于遇到同样问题的每个人。
我的问题出在我用点击功能给出的值内。我第一次
(click)="submitComment(comment)"
但 'comment' 未定义。我以为我的 ngFor="let comment of card.comments" 会定义它,但我的点击功能在 ngFor 之外。因此我不得不创建
(click)="submitComment(card)"
在我的打字稿文件中如下:
submitComment(card) {
console.log(this.newComment);
card.comments.push(this.newComment);
}