【问题标题】:Using a selected value in <option> Angular.js在 <option> Angular.js 中使用选定的值
【发布时间】:2015-01-24 09:47:00
【问题描述】:

我刚刚开始学习使用 Angular.js,但我不知道这个:

我想显示数组中选定的value ="song.title" 与数组中的song.title 匹配的项目。

这是我的代码

<select>
     <option ng-repeat="song in songs.tracks">{{song.title}}</option>
</select>

现在我想在此显示更多选定的歌曲数组:

<section ng-repeat="song in songs.tracks">
     <div class="panel" ng-show="panel.isSelected(1)" >

<!-- in here i wanna show stuff from the array of the selected song -->

     </div>

【问题讨论】:

    标签: javascript arrays angularjs angularjs-ng-repeat angularjs-ng-model


    【解决方案1】:

    结帐this

     <div>
      Select Song :: <select ng-model="selectedSong" ng-options="song as song.title for song in songs.tracks">
      </select>
    
      </div>
      <div>
      Selected Song :: 
    
      {{selectedSong}}
      </div>
    

    【讨论】:

    • 非常感谢!我还没有遇到 ng-options 表达式,你能解释一下它是如何工作的吗?
    • 你知道我需要什么条件才能让我的 ng-click 到一个按钮,以便在单击它时显示“歌曲”数组中的第一项吗?
    【解决方案2】:

    所以,首先你必须将模型附加到你的选择标签上,使用ng-model 指令。在option 标签集属性value="{{song.id}}" 所以你的模型将包含选定的歌曲ID。

    第二件事,您的循环显示 div 与数组中的内容是错误的,因为在每个循环中您将 1 传递给您的 isSelected 方法。更好的是isSelected($index)。但是您不必执行此循环,只需使用包含歌曲数组中所选 id 的模型即可

    <div>{{songs[yourmodel].title}}</div>
    

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多