简体   繁体   中英

How to store a angular.js model variable in a javascript variable?

How can I store let's say selectedSong.artist in a variable in my javascript code ? I want to do this so i can use this selected value as a search term in another function . the {{selectedSong.artist}} epression is giving back a name , I want to put this name in a variable in my 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 now lives in your controller (assuming there is a controller somewhere above this code). You can then use a $watch function to get that variable whenever it changes, or just reference it directly from your controller.

Here is a $watch example:

      $scope.$watch('selectedSong', function (newVal, oldVal) {
         if (newVal !== oldVal) {
            // Do something with newVal It will be selectedSong
         }
      });

Or just as a reference:

$scope.selectedSong

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