简体   繁体   中英

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. 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.

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:

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.

The model will now have the ID as the value. Therefore, you can now set it directly:

$scope.v = data.ValveId;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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