Javascript 更改数组中的值时,ng选项不会更新
我想更新选择列表中的数据,但选择不更新,但当我选择“测试”时,会选择api中的值,即使它未列出 例如: 这是我的代码…Javascript 更改数组中的值时,ng选项不会更新,javascript,angularjs,Javascript,Angularjs,我想更新选择列表中的数据,但选择不更新,但当我选择“测试”时,会选择api中的值,即使它未列出 例如: 这是我的代码… <div ng-controller="selectController" class="form-group text-primary col-lg-4 col-md-6 col-sm-6 col-xs-12"> <label for="voivodeship-accou
<div ng-controller="selectController"
class="form-group text-primary col-lg-4 col-md-6 col-sm-6 col-xs-12">
<label for="voivodeship-account-creator">Województwo</label>
<select class="selectpicker form-control-select"
data-live-search="true" data-size="5"
name="voivodeship-account-creator"
id="voivodeship-account-creator"
ng-options="region.name for region in regions track by region.id" ng-model="selectedRegion">
<option data-hidden='true'></option>
</select>
</div>
JSON:
看起来像是一个角度范围问题。Angular可能已为选择零件创建了子范围。您应该将属性附加到一个对象,这将允许您使用范围继承。所以在你的控制器上
$scope.selectedRegion = {selection: ""};
然后在html中,更改ng模型以反映它现在是对象而不是原语。(将来,尽量不要在范围对象上使用原语,这样就不必担心处理范围界定问题)
您缺少ng型号
$scope.defaultSelected = {"id":1,"name":"SSS"};
$scope.regions = [{"id":1,"name":"Śląsk"},{"id":2,"name":"Małopolska"},{"id":3,"name":"Mazowsze"}]
$scope.addItem = function() {
$scope.regions.push({"id":4,"name":"Dlląsk"});
}
HTML
<select class="selectpicker form-control-select"
data-live-search="true" data-size="5"
name="voivodeship-account-creator"
id="voivodeship-account-creator"
ng-model="defaultSelected"
ng-options="region.name for region in regions track by region.id" ng-model="selectedRegion">
<option data-hidden='true'></option>
</select>
<button ng-click="addItem()">Add</button>
添加
请提供一个JSFIDLE。但他已经有了一个ng模型。选择元素现在有两个ng模型。
<select class="selectpicker form-control-select"
data-live-search="true" data-size="5"
name="voivodeship-account-creator"
id="voivodeship-account-creator"
ng-options="region.name for region in regions track by region.id" ng-model="selectedRegion.selection">
$scope.defaultSelected = {"id":1,"name":"SSS"};
$scope.regions = [{"id":1,"name":"Śląsk"},{"id":2,"name":"Małopolska"},{"id":3,"name":"Mazowsze"}]
$scope.addItem = function() {
$scope.regions.push({"id":4,"name":"Dlląsk"});
}
<select class="selectpicker form-control-select"
data-live-search="true" data-size="5"
name="voivodeship-account-creator"
id="voivodeship-account-creator"
ng-model="defaultSelected"
ng-options="region.name for region in regions track by region.id" ng-model="selectedRegion">
<option data-hidden='true'></option>
</select>
<button ng-click="addItem()">Add</button>