Angualrjs下拉选择框读取Json对象文本

时间:2015-07-19 03:40:33

标签: json angularjs angularjs-service

我要做的是创建一个从json数据中读取文本的下拉选项。我不知道怎么读不到价值,我不知道为什么。

这是我的代码

我的下拉框:

   <label class="item item-input item-select">
                <b class="input-label">Claim Type:</b>
                <select ng-model="claimType" required>
                  <option value="Select claim" title="Select Claim" selected disabled>Claim Type</option>
                  <option ng-repeat="claim in claimType " value="{{claim.value}}"
                          ng-selected="{{claim.value== claimType}}">
                          {{claim.text}} 
                              </option>
                            </select>
                        </label>

我的Json对象

 angular.module('app')
        .factory('WebApi', function () {

            var claimType = [{
                value: "Car",
                text: "Car",
            }, {
                value: "Boat",
                text: "Boat",
            }, {
                value: "Others",
                text: "Others"
            }]
        })
 //Display 50 items randomly
   var tempData = [];
        for (var i = 0; i < 50; i++) {
                    var selectedClaimType = claimType[Math.floor((Math.random() * claimType.length))];
                   tempData.push({
                        claimType: selectedClaimType.text,
                    })
                };
            return {
              getClaimTypes: function () {

                    return selectedClaimType.text;
                },
            }

在我的控制器中,我打电话给

$scope.claimType = WebApi.getClaimTypes();

1 个答案:

答案 0 :(得分:0)

您可以使用ng-options来帮助管理选择列表。

<label class="item item-input item-select">
    <b class="input-label">Claim Type:</b>
    <select ng-model="claimType" required
      ng-options="transport.text for transport in traffic">
      <option value="" title="Select Claim" selected disabled>Claim Type</option>
    </select>
</label>

这会将选定的transport绑定到claimType。如果您希望为claimType分配值transport.value,则可以执行以下操作:

<label class="item item-input item-select">
    <b class="input-label">Claim Type:</b>
    <select ng-model="claimType" required
      ng-options="transport.text as transport.value for transport in traffic">
      <option value="" title="Select Claim" selected disabled>Claim Type</option>
    </select>
</label>