[英]How can I select an item from a select list after populating it?
I have the following HTML:我有以下 HTML:
<span>SubTopic {{ modal.option.sSubTopic }}</span>
<select data-ng-model="modal.option.sSubTopic"
data-ng-options="item.id as item.name for item in modal.option.subTopics">
</select>
In my controller在我的控制器中
$http.get(url, { cache: us.oneDayCache })
.success(function (data) {
$scope.modal.option.subTopics = data;
$scope.modal.option.sSubTopic = "2";
})
After the get the {{ }} value is set to 2 but the 2nd item of my select is not selected. get {{ }} 值设置为 2 后,但未选择我选择的第二项。
Is there something wrong with the way I am doing this?我这样做的方式有问题吗?
You are specifying the value of the field to be the id
of the item with item.id as
in the ngOptions
.您正在将字段的值指定为
item.id as
的项目的id
, item.id as
ngOptions
。 The model value needs to match the value of the data
id
you want selected.模型值需要与您要选择的
data
id
的值相匹配。 If you want the second object, it would be data[1].id
如果你想要第二个对象,那就是
data[1].id
$http.get(url, { cache: us.oneDayCache })
.success(function (data) {
$scope.modal.option.subTopics = data;
$scope.modal.option.sSubTopic = data[1].id;
});
Demo: http://jsfiddle.net/TheSharpieOne/gteX9/2/演示: http : //jsfiddle.net/TheSharpieOne/gteX9/2/
The data-ng-model contains the model in which the selected value will be stored. data-ng-model 包含将存储所选值的模型。 In your case this would mean you'd store the selected value in 2. That doesn't seem right.
在您的情况下,这意味着您会将所选值存储在 2 中。这似乎不对。 Perhaps you could try something like:
也许您可以尝试以下操作:
<select ng-model="selectedItem" ng-options="item.id as item.name for item in modal.options.subTopics"></select>
<pre>{{selectedItem}}</pre>
$scope.selectedItem contains your selected item. $scope.selectedItem 包含您选择的项目。 Now you want to select the second item in your data object.
现在您要选择数据对象中的第二项。 You could do that by doing this in your controller:
您可以通过在控制器中执行此操作来做到这一点:
$http.get(url, { cache: us.oneDayCache })
.success(function (data) {
$scope.modal.option.subTopics = data;
$scope.selectedItem = data[1];
})
Wasn't able to test it here, but it should work.无法在这里测试它,但它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.