简体   繁体   English

如何在data-ng-options创建的下拉列表中设置默认选定选项

[英]How to set the default selected option in a dropdown being created by data-ng-options

I have a select dropdown which is being setup and the options created via 'data-ng-options' 我有一个正在设置的选择下拉列表和通过'data-ng-options'创建的选项

This is the HTML: 这是HTML:

<select class="form-control" id="playListOpt" ng-model="playListItem.selectedObject" ng-change="changePlaylist(playListItem.selectedObject)" ng-required="true"
              data-ng-options="playListItem as playListItem.name for playListItem in playListDataForActivity">
        <option value="" translate="PLAYLIST_CREATE_NEW"></option>
      </select>

This produces the following output in the compiled html: 这会在编译的html中生成以下输出:

<select class="form-control ng-pristine ng-valid ng-valid-required ng-touched" id="playListOpt" ng-model="playListItem.selectedObject" ng-change="changePlaylist(playListItem.selectedObject)" ng-required="true" data-ng-options="playListItem as playListItem.name for playListItem in playListDataForActivity" required="required" aria-required="false" aria-invalid="false" style=""><option value="" translate="PLAYLIST_CREATE_NEW" class="" selected="selected">Create New Playlist</option><option label="Dev Playlist 1" value="object:2670">Dev Playlist 1</option><option label="Dev Playlist 2 (Favorite)" value="object:2671">Dev Playlist 2 (Favorite)</option></select>

Currently the option 'Create New Playlist' is always the selected option. 目前,“创建新播放列表”选项始终是所选选项。 However i'd like to set the selected option to be the one which is set to 'isDefault' which is available to me via playListItem. 但是我想将所选的选项设置为设置为'isDefault'的选项,我可以通过playListItem使用该选项。 I feel my issue is not understanding how to set the selectedObject since the value of the option seems to be getting generated by the code? 我觉得我的问题是不了解如何设置selectedObject,因为选项的值似乎是由代码生成的?

This is the code in the controller where if the item is set to 'isDefault' that i'm setting the selectedObject. 这是控制器中的代码,如果项目设置为'isDefault',我正在设置selectedObject。 This doesn't work. 这不起作用。

if (response && response.status == 200) {
              $scope.playListDataForActivity = response.data.playlists;
              $scope.totalOwnedPlaylistCount = response.data.totalOwnedPlaylistCount;

              for (var i = 0; $scope.playListDataForActivity[i]; i++) {
                if ($scope.playListDataForActivity[i].isDefault)
                  $scope.playListItem.selectedObject = $scope.playListDataForActivity[i];
                }
          }

Any tips on what I need to do to fix this? 我需要做些什么才能解决这个问题?

Updated code above and new question below 更新了上面的代码和下面的新问题

After the change suggested below by @Lex both the 'Create New Playlist' and the IsDefault option are being flagged as selected in the HTML 在@Lex下面建议的更改之后,“创建新播放列表”和IsDefault选项都在HTML中被标记为已选中

<select class="form-control ng-pristine ng-valid ng-valid-required ng-touched" id="playListOpt" ng-model="playListItem.selectedObject" ng-change="changePlaylist(playListItem.selectedObject)" ng-required="true" data-ng-options="playListItem as playListItem.name for playListItem in playListDataForActivity" required="required" aria-required="false" aria-invalid="false" style=""><option value="" translate="PLAYLIST_CREATE_NEW" class="" *selected="selected"*>Create New Playlist</option><option label="Dev Playlist 1" value="object:2996">Dev Playlist 1</option><option label="Dev Playlist 2 (Favorite)" value="object:2995" *selected="selected"*>Dev Playlist 2 (Favorite)</option><option label="Dev Playlist 3" value="object:2997">Dev Playlist 3</option></select>

Is there something I need to clear, or is there something in the 'select' that needs to be added? 有什么我需要清楚的,或者是否需要添加“选择”中的某些内容?

for updated code and new question, If you have particular value set as default selected value, then no need to have <option value="" translate="PLAYLIST_CREATE_NEW"></option> as data-ng-options is being used. 对于更新的代码和新问题,如果您将特定值设置为默认选择值,则不需要使用<option value="" translate="PLAYLIST_CREATE_NEW"></option>作为data-ng-options。 By removing option, it will get solved. 通过删除选项,它将得到解决。 Hope this helps! 希望这可以帮助!

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

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