[英]How to store a angular.js model variable in a javascript variable?
我该如何在我的javascript代码的变量中存储selectedSong.artist? 我想这样做,以便可以在另一个函数中将此选定的值用作搜索项。 {{selectedSong.artist}}表达式返回了一个名称,我想将此名称放入javascript中的变量中。
<div class="list-group-item col-md-12">
<div class="col-md-6">
<p>
Titel : <select ng-model="selectedSong" ng-options="song as song.title for song in songs.tracks"></select>
</p>
<p>
Artiest : <select ng-model="selectedSong" ng-options="song as song.artist.name for song in songs.tracks"></select>
</p>
<p>
Tijdstip : <select ng-model="selectedSong" ng-options="song as (song.played_at | date: 'hh:mm') for song in songs.tracks">
</select>
</p>
</div>
<div class="col-md-6">
<button class="btn-success btn-lg" ng-click="selectedSong = songs.tracks[0]">Het afspelende lied</button>
</div>
</div>
selectedSong现在位于您的控制器中(假设此代码上方某处有一个控制器)。 然后,您可以使用$ watch函数在变量更改时获取该变量,或者直接从您的控制器中引用它。
这是一个$ watch示例:
$scope.$watch('selectedSong', function (newVal, oldVal) {
if (newVal !== oldVal) {
// Do something with newVal It will be selectedSong
}
});
或仅供参考:
$scope.selectedSong
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.