AngularJs - 从集合属性设置ng-model

时间:2017-07-10 21:16:25

标签: javascript angularjs html-select ng-options angularjs-ng-model

我遇到这个相当奇怪的问题,为选择下拉列表设置ng-model

我用于ng-model的属性值似乎与ng-options中的条目匹配,但ng-model始终最终为null

这是获取订单的方法:

orderService.getMerchantOrders(qs)
            .then(
            function (response) {
                $scope.isLoading = false;
                $scope.pagerService = new pagerService({
                    page: $scope.pagerService.page,
                    data: response.data.items,
                    total: response.data.total,
                    sortVars: response.data.sort,
                    pageSize: 5
                });
            },
            function (error) {
                if (error.status === 401) {
                    $window.location.href = $scope.returnUrl;
                } else {
                    //show error message
                    console.log(error);
                }
            });

这是pagerService.data的样子: enter image description here

order.orderShippingMethod[0].shippingMethod的值为:

{"shippingMethodId":7,"carrierName":"Russian Post","carrierUrl":"http://www.russianpost.ru/tracking20/English.htm","orderShippingMethod":[]}

选择列表值为: enter image description here

感谢您的任何想法。我是 AngularJs 的初学者,所以我觉得这很简单,我在这里不见了!

<select class="form-control" name="carrierList"

ng-model="order.orderShippingMethod[0].shippingMethod"

ng-options="shippingMethod.shippingMethodId as shippingMethod.carrierName 
for shippingMethod in shippingMethods" required>

<option value="">Select Carrier</option>

</select>

1 个答案:

答案 0 :(得分:1)

ngOptions跟踪语法替换为 id 作为名称

shippingMethod.carrierName for shippingMethod in shippingMethods track by shippingMethod.shippingMethodId

见下文演示:

&#13;
&#13;
angular.module('myApp', [])
  .controller('ctrl', function($scope) {
    $scope.shippingMethods = [{
        "shippingMethodId": 7,
        "carrierName": "Russian Post",
        "carrierUrl": "http://www.russianpost.ru/tracking20/English.htm",
        "orderShippingMethod": []
      },
      {
        "shippingMethodId": 8,
        "carrierName": "Persian Post",
        "carrierUrl": "http://www.russianpost.ru/tracking20/English.htm",
        "orderShippingMethod": []
      }
    ];
    $scope.order = {
      orderShippingMethod: [{
        "shippingMethod": {
          "shippingMethodId": 8,
          "carrierName": "Persian Post",
          "carrierUrl": "http://www.russianpost.ru/tracking20/English.htm",
          "orderShippingMethod": []
        }
      }]
    };
  })
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
  <select class="form-control" name="carrierList" ng-model="order.orderShippingMethod[0].shippingMethod" ng-options="shippingMethod.carrierName 
for shippingMethod in shippingMethods track by shippingMethod.shippingMethodId" required>

<option value="">Select Carrier</option>

</select> 
<div>
order.orderShippingMethod[0].shippingMethod: {{ order.orderShippingMethod[0].shippingMethod }}</div>
</div>
&#13;
&#13;
&#13;