在Select Attribute中打印数组元素

时间:2016-04-02 11:12:24

标签: angularjs

我有一系列数量[250克,500克,1KG,2件,4件]来自服务器端。我想在select属性的jsp中显示这个数组。我怎么能用angularjs做到这一点?

数组内的元素可能会有所不同,具体取决于来自服务器端的值。

这是我从服务器端获取的完整数组:

[{“price”:40,“name”:“Apple”,“primaryKey”:21701,“quantity”:“[250克,500克,1KG,2件,4件]”},{“价格“:0,”名称“:”香蕉“,”primaryKey“:21705,”数量“:”[250克,500克,1KG]“},{”价格“:0,”名称“:”石榴“ ,“primaryKey”:21709,“quantity”:“[250 Gram,500 Gram]”},{“price”:0,“name”:“Orange”,“primaryKey”:21713,“quantity”:“[250克,500克,1KG,2KG]“}]

现在,我想要的是在select属性中显示数量部分。

1 个答案:

答案 0 :(得分:0)

你想要这样的东西吗?

编辑:请记住,这不仅仅是真正的解决方案。一个真正的解决方案是从服务器端获取适当的值。如果您无法访问服务器端,那么这应该没问题。



var app = angular.module('theApp', []);
app.controller('myctrlR', function($scope) {
  $scope.SelectedItem = [];
  $scope.items = [{
    "price": 40,
    "name": "Apple",
    "primaryKey": 21701,
    "quantity": "[250 Gram, 500 Gram, 1KG, 2 pieces, 4 pieces]"
  }, {
    "price": 0,
    "name": "Banana",
    "primaryKey": 21705,
    "quantity": "[250 Gram, 500 Gram, 1KG]"
  }, {
    "price": 0,
    "name": "Pomegranate",
    "primaryKey": 21709,
    "quantity": "[250 Gram, 500 Gram]"
  }, {
    "price": 0,
    "name": "Orange",
    "primaryKey": 21713,
    "quantity": "[250 Gram, 500 Gram, 1KG, 2KG]"
  }]

  angular.forEach($scope.items, function(val, key) {
    val.quantity = val.quantity.replace('[', '').replace(']', '').split(', ')
  })
})

<html ng-app="theApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js">
</script>
<div ng-controller="myctrlR">
  <div ng-repeat="item in items" style="margin-bottom:20px;" ng-init="SelectedItem[$index]=''">
    <div>{{item.name}} - Price: ${{item.price}}</div>
    <select ng-model="SelectedItem[$index]" ng-options="x for x in item.quantity track by x">
      <option value="">Select Item</option>
    </select>
    <div>Selected Item is: <strong>{{SelectedItem[$index]}}</strong>
    </div>
  </div>
</div>

</html>
&#13;
&#13;
&#13;