检索具有角度的选定选项文本,而不仅仅是包含id的值

Retrieve selected option text with angular, not only the value that contains the id

本文关键字:文本 不仅仅是 包含 的值 id 选项 检索      更新时间:2023-09-26

我想知道检索<option></option>之间的文本的最佳方式是什么,因为分配给<select>的模型只返回id。

<select class="form-control" ng-model="videos.playlist">
    <option ng-repeat="playlist in playlists" value="@{{ playlist.id }}"
            ng-selected="savePlaylistName()">
        @{{ playlist.name }}
    </option>
</select>

如前所述,videos.playlist包含playlist.id,但我怎么能在变量中也包含playlist.name呢?我唯一能想到的就是使用ng-selected并进行处理,但它比我预期和/或期望的要复杂一些。那么,有什么想法可以轻松实现这一点吗?

理想情况下,我会有类似videos.playlist.idvideos.playlist.name的东西,因为我手头也需要id

试试这个:

<select ng-model="videos.playlist" ng-options="playlist.id for playlist.name in playlists">
      <option value="">-- choose playlist --</option>
</select>

将ng选项与表达式一起使用:

<select ng-model="videos.playlist" ng-options="playlist.name for playlist in playlists">
      <option value="">-- choose playlist --</option>
</select>

播放列表对象是将被绑定到video.playlist的所选对象。