JSON
{
"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();
}
Above I have posted my customize code. But my Add and Remove not not working. Please check my JSON this data I am getting from my database.
Please help me.
$scope.product_attributesand$scope.attributes, which is confusing, and you point to$scope.attributes.choicesbutchoicesis a property of a singleattribute, not the array. You'd what$scope.attributes[0].choicesor whatever.