简体   繁体   English

无法使用AngularJS在下拉菜单中设置所选选项

[英]Can't set selected option in dropdown with AngularJS

I've this simple dropdown: 我有一个简单的下拉列表:

<select ng-model="v" ng-options="valve.Brand + ' ' + valve.Model + ' ' + valve.Notes for valve in valves track by valve.ValveId">
    <option value="">--Valves--</option>
</select>

I'm also using a web service to retrieve data from database. 我还使用Web服务从数据库检索数据。 This data contains the ValveId that it's the parameter (value) I would like to use to set as selected an option of my dropdown. 该数据包含ValveId ,它是我要用来设置下拉菜单选项的参数(值)。

I've tried this way from my controller: 我从控制器尝试过这种方式:

$scope.v = $scope.valves[data.ValveId];

But it is not working. 但这是行不通的。

Any suggestion? 有什么建议吗?

Replace your ng-options by: ng-options替换为:

valve.ValveId as valve.Brand + ' ' + valve.Model + ' ' + valve.Notes for valve in valves

Notice the as key at the beginning, it tells which property should be used as the model value. 请注意开头的as键,它指示应将哪个属性用作模型值。

The model will now have the ID as the value. 该模型现在将具有ID作为值。 Therefore, you can now set it directly: 因此,您现在可以直接设置它:

$scope.v = data.ValveId;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM