【问题标题】:AngularJs - Set ng-model from collection propertyAngularJs - 从集合属性设置 ng-model
【发布时间】:2017-07-10 21:16:24
【问题描述】:

我在为选择下拉列表设置 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 如下所示:

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

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

选择列表值是:

感谢您的任何想法。我是 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>

【问题讨论】:

  • 您应该添加一些代码来说明您当前围绕您提供的 sn-p 所拥有的内容。您应该显示 $scope.order 的定义位置,以及 $scope.order.orderShippingMethod(s) 的定义位置。
  • 好主意,更新了更多细节!

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


【解决方案1】:

使用ngOptionstrack by 语法代替id 作为name

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>

【讨论】:

  • 漂亮!我知道这很简单,谢谢@Sam Onela!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
相关资源
最近更新 更多