【发布时间】:2017-06-03 20:50:47
【问题描述】:
我在 JSON 中选择了来自后端的值,并且我也在同一个 JSON 中选择了来自后端的元素内容。
问题是在选择元素中没有选择选定的值,但是当我选择新元素时绑定工作正常。
我尝试了几种方法(选项元素 ng-repeat、ng-option),结果都相同。选择元素中未选择 data.cruiseCategoryId 中的选定值。
我的 app.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.data = {
cruiseCategoryDropdownOptions: [{
disabled: false,
group: null,
selected: false,
text: "Interior Cabin Bella",
value: "6"
}, {
disabled: false,
group: null,
selected: false,
text: "Interior Cabin Fantastica",
value: "7"
}, {
disabled: false,
group: null,
selected: false,
text: "Oceanview Cabin Fantastica",
value: "8"
}, {
disabled: false,
group: null,
selected: false,
text: "Balcony Bella",
value: "9"
}, {
disabled: false,
group: null,
selected: false,
text: "Balcony Fantastica",
value: "10"
}, {
disabled: false,
group: null,
selected: false,
text: "Balcony Aurea",
value: "11"
}, {
disabled: false,
group: null,
selected: false,
text: "Balcony Wellness",
value: "12"
}, {
disabled: false,
group: null,
selected: false,
text: "Suite Fantastica",
value: "13"
}, {
disabled: false,
group: null,
selected: false,
text: "Suite Aurea",
value: "14"
}, {
disabled: false,
group: null,
selected: false,
text: "Suite Yacht Club Deluxe",
value: "15"
}],
cruiseCategoryId: 10
}
});
我的html
selected value: {{data.cruiseCategoryId}}
<select class="form-control"
name="cruiseCategoryId"
id="cruiseCategoryId"
ng-options="i.value as i.text for i in data.cruiseCategoryDropdownOptions track by i.value"
ng-model="data.cruiseCategoryId">
</select>
【问题讨论】:
-
取回数据后执行 $scope.$apply() 将触发摘要函数来更新模型
标签: angularjs angularjs-directive angularjs-ng-repeat ng-repeat ng-options