我想添加一个条目到我的评论数组。Angular JS表单提交问题
<b>Submit a review</b>
<form name="submitReviews" ng-controller="ReviewController as reviewCtrl" ng-submit="reviewCtrl.addReview(product)">
<blockquote>
<b>Stars : {{reviewCtrl.review.stars}}</b>
{{reviewCtrl.review.body}}
<cite>by: {{reviewCtrl.review.author}}</cite>
</blockquote>
<select ng-model="reviewCtrl.review.stars">
<option value="1">1 Star</option>
<option value="2">2 Star</option>
<option value="3">3 Star</option>
<option value="4">4 Star</option>
<option value="5">5 Star</option>
</select>
<textarea ng-model="reviewCtrl.review.body">
</textarea>
<label>by: </label>
<input ng-model="reviewCtrl.review.author" type="email" />
<input type="submit" value="Submit Review" />
</form>
JS文件:
app.controller('ReviewController', function($scope) {
$scope.review = {};
$scope.addReview = function (product) {
product.reviews.push($scope.review);
$scope.review = {};
};
});
的阵列,而我想补充(完整的对象结构):
var gems = [{
//First gem
name: "Dodecahedron",
price: 2.95,
description: "great stone",
specification: "Very Expensive, hand crafted, African made",
reviews: [{
stars: 5,
body: "test review1",
author: "[email protected]",
},
{
stars: 3,
body: "not worth the price.",
author: "[email protected]"
}],
canPurchase : true,
soldOut: false,
image: [
{ image1 : "Images/img1.jpeg" },
{ image2 : "Images/img2.jpeg" }
]
}
我可以看到正确的表格前的所有信息(在数组中预定义的图像和当前评论)。当我从HTML页面提交新评论时,应该将其添加到现有评论(arr.push)中。这没有发生。 该控制器(ReviewController)嵌套在另一个控制器内。不知道这是否有所作为。
我认为你需要将product.reviews声明为一个数组,作为product.reviews = [];试试这个 – Aravind