[英]Cannot read property 'comments' of undefined error in angularjs
我也問過這個問題。 也許有人可以在這里捕獲錯誤。
.controller('DishDetailController', ['$scope', function($scope) {
var dish={
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}
]
};
$scope.dish = dish;
}])
.controller('DishCommentController', ['$scope', function($scope) {
$scope.newcomment = {
rating : "",
comment: "",
author: "",
date: new Date().toISOString()
};
$scope.submitComment = function () {
$scope.newcomment.date = new Date().toISOString();
$scope.newcomment.rating = parseInt($scope.newcomment.rating)
console.log($scope.newcomment.comment);
console.log($scope.newcomment.author);
console.log($scope.newcomment.rating);
console.log($scope.newcomment.date);
$scope.dish.comments.push($scope.newcomment);
}
}]);
在html部分中,我只有兩三個文本框。 在按鈕onclick上的SubmitComment
。 Console.log向我顯示所需的結果。 但是我無法將其推送到數組。 該練習是教程的一部分。 因此,需要將它們編寫在單獨的控制器中,並且由教程提供
碟形對象未在第二個控制器中定義,這從錯誤“未定義”中可以理解。 您必須找到一種方法,將碟形對象從第一個控制器“ DishDetailController”傳遞到第二個“ DishCommentController”。 有幾種方法可以做到這一點,一種方法是將對象存儲到第一個控制器的服務中,然后從第二個控制器的同一服務中檢索它。 據我所知,在控制器之間共享對象的最佳方法是通過服務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.