我想添加一個條目到我的評論數組。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