gpt4 book ai didi

javascript - 选择选项不与 ng-model 绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 08:32:10 26 4
gpt4 key购买 nike

我有一个包含 AllCountries 和 SelectedCoutry 的对象。我想在 <select> 上列出所有国家/地区并通过 SelectedCountry 的值选择带有 ng-model 的选项。

但是组合框的默认值被选中为空。

例子:

angular.module('ngPatternExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.OBJ = {
"OBJ": {
"AllCountries": [
{
"country": "USA",
"id": 1
},
{
"country": "Mexico",
"id": 2
},
{
"country": "Canada",
"id": 3
}
],
"SelectedCountry": {
"country_id": 1,
}
}
}

$scope.AM = $scope.OBJ.OBJ;
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="ngPatternExample">
<div ng-controller="ExampleController">
selected country: {{AM.SelectedCountry.country_id}} <br/>
<select class="form-control" ng-model="AM.SelectedCountry.country_id">
<option value=""> --- </option>
<option ng-repeat="co in AM.AllCountries" value="{{co.id}}"> {{co.country}} </option>
</select>
</div>
</body>

( http://plnkr.co/edit/PI3tOrIMSTMwGC0rYA5q?p=preview )

p.s 一个很好的解释为什么这在 Angular 中总是不起作用会很棒

最佳答案

用这个替换你的选择

<select class="form-control"  ng-options="co.id as co.country for co in OBJ.OBJ.AllCountries" ng-model="AM.SelectedCountry.country_id">
<option value=""> --- </option>
</select>

关于javascript - 选择选项不与 ng-model 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36080406/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com