angularjs - How to toggle between a play and pause button? -


essentially, know best way toggle between 2 img or div on user click. so, first play image visible , when user clicks, play image hidden , pause image shown. when user clicks pause image, pause image hidden , play image shown.

i tried using ng-show figure out how show both images without hiding other. so, play image next pause image, not ideal.

thanks in advance :)

code have tried not work:

html

<button ng-click="changestatus" ng-show="checkstatus">play</button> <button ng-click="changestatusagain" ng-hide="checkstatus">pause</button> 

controller:

$scope.changestatus=function(){     $scope.checkstatus = false; }  $scope.changestatusagain=function(){     $scope.checkstatus = true; } 

https://jsfiddle.net/n8nnwtte/

use same expression both , negate according logic.

html

 <button class="play" ng-show="!song.playing" ng-click="song.toggleplay()">  <button class="pause" ng-show="song.playing" ng-click="song.toggleplay()"> 

js

var $scope.song = {     playing: false, // set initial state      toggleplay: function() {          $scope.song.playing = !$scope.song.playing; // alternate state each time           // song         if ($scope.song.playing) { /*..*/ }  else { /*..*/ }     } }; 

this assumes have song object defined in scope , has boolean property called playing. if song.playing falsy show play button , if it's truthy show pause button.

the toggleplay() method defined can used alternate state of song.playing in addition role of controlling playback.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -