私はレビューアレイにエントリーを追加したいと思います。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