代码之家  ›  专栏  ›  技术社区  ›  Mike Anderson

AngularJs-从集合属性设置ng模型

  •  0
  • Mike Anderson  · 技术社区  · 7 年前

    我在设置 ng-model 用于选择下拉列表。

    ng模型 ng-options 总是以 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的功能。数据如下所示: 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 回复  |  直到 7 年前
        1
  •  1
  •   Sᴀᴍ Onᴇᴌᴀ    7 年前

    使用 语法 ngOptions 而不是 身份证件 名称 :

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

    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": []
            }
          }]
        };
      })
    <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>