JSONを動作していない動的な追加やdiv要素を削除AngularJS
{
"id": "1",
"name": "T-Shirt",
"status": "1",
"product_attributes": [{
"id": 1,
"label": "Size",
"choices": [{
"text": "Size 30",
"value": "Size 30",
"isSelected": false,
"price": "$100.00"
}, {
"text": "Size 32",
"value": "Size 32",
"isSelected": true,
"price": "$100.00"
}, {
"text": "Size 34",
"value": "Size 34",
"isSelected": false,
"price": "$100.00"
}, {
"text": "Size 36",
"value": "Size 36",
"isSelected": false,
"price": "$100.00"
}]
}, {
"id": 2,
"label": "Color",
"choices": [{
"text": "Denim",
"value": "Denim",
"isSelected": true,
"price": "$0.00"
}, {
"text": "Black",
"value": "Black",
"isSelected": false,
"price": "$5.00"
}, {
"text": "Brown",
"value": "Brown",
"isSelected": false,
"price": "$2.00"
}],
}]
}
私は私のカストを掲載している上HTML
<div ng-repeat="attributes in product.product_attributes">
<h3>{{attributes.name}}</h3>
<div class="choice">
<h2>Choices</h2>
<div ng-repeat="choices in attributes.choices">
<div class="form-group">
<input type="text" ng-model="choices.value" class="form-control">
<a href="" ng-click="addField()">Add</a>
<a href="" ng-click="removeField($index)">Remove</a>
</div>
</div>
</div>
</div>
JS
$scope.attributes = {choices: [{label:'1'}]};
$scope.getProductAndAttributes = function() {
$http({
method: 'POST',
url: 'products/get_product_details.json',
dataType: "json",
data: {'id': $stateParams.product_id},
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function(data) {
$scope.product_name = data.name;
$scope.product_attributes = data.product_attributes;
});
};
$scope.addField = function() {
var newItemNo = $scope.attributes.choices.length + 1; alert(newItemNo);
$scope.attributes.choices.push({'label': 'choice' + newItemNo});
};
$scope.removeField = function(i) {
$scope.attributes.choices.splice(i, 1);
};
if ($stateParams.product_id) {
$scope.getProductAndAttributes();
}
コードを省略する。しかし私のAdd
とRemove
は機能しません。私のデータベースから取得している私のJSON
のデータをチェックしてください。
私を助けてください。
は "かなり働いていません "。私たちはあなたが何を追加または削除することを期待しているか分かりません。 '$ scope.product_attributes'と' $ scope.attributes'の両方が混乱していて、 '$ scope.attributes.choices'を指していますが、' choices'は単一の 'attribute'のプロパティです、配列ではありません。あなたは '$ scope.attributes [0] .choices'なんかどんなものでもあります。 –