{
"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'或其他什麼。 –