我遇到这个相当奇怪的问题,为选择下拉列表设置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);
}
});
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>
答案 0 :(得分:1)
将ngOptions的跟踪语法替换为 id 作为名称:
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": []
}
}]
};
})
&#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;