循环内的角度设置模态值

时间:2015-10-20 05:39:42

标签: javascript html angularjs

我有角度应用程序,用户动态表单字段添加

HTML

<div class="sub_skus">
  <div class="select-style">
    <select data-ng-options="unitP as unitP.name for unitP in unitProducts" data-ng-model="choice.subProduct[choice.id]" data-ng-change="getUnitProductData(choice.id)">
    </select>
  </div>
</div>

<div class="sub_skus">
  <input type="text" data-ng-model="choice.subSku">
</div>
<div class="sub_skus">
  <input type="text" data-ng-model="choice.price" placeholder="Price">
</div>
<div class="sub_skus">
  <input type="number" data-ng-model="choice.quantity" placeholder="Quantity">
</div>

<div>
  <span ng-show="$last" ng-click="removeChoice()" class="input-group-addon a1 vd_bg-red vd_white rmMainIcon">
      <i class="fa fa-minus"></i>
   </span>
</div>
</div>

JS

$scope.unitProducts = {};
productService.getUnitProducts().then(function success(responce) {
  $scope.unitProducts = responce.data.units;
  console.log($scope.unitProducts);
});

$scope.choices = [{
  id: '1'
}];

$scope.addNewChoice = function() {
  var newItemNo = $scope.choices.length + 1;
  $scope.choices.push({
    'id': newItemNo
  });
};

$scope.removeChoice = function() {
  var lastItem = $scope.choices.length - 1;
  $scope.choices.splice(lastItem);
};

当我从select中选择一个选项时,它将从服务

获得以下数据
[{"id":"1","subProduct":{"1":{"id":"15","name":"rrrr","sku":"rf","price":"3.00"}}}]

我想在循环内将上述数据设置为 sku choice.subSku并将价格设置为choice.price吗?

1 个答案:

答案 0 :(得分:1)

我建议请试试JSON.simple是一个简单的Java库,用于JSON处理,读写JSON数据以及完全符合JSON规范(RFC4627)。

网上有很多可用的例子。请冲浪。我还附加了一些有用的 堆栈流和其他链接

java-iterate-over-jsonobject - How to iterate over a JSONObject?

json-simple-example-read-and-write-json - http://www.mkyong.com/java/json-simple-example-read-and-write-json/

how-to-access-nested-elements-of-json-object-using-getjsonarray-method - How to access nested elements of json object using getJSONArray method

how-can-i-iterate-jsonobject-to-get-individual-items - How can I iterate JSONObject to get individual items