In AngularJS how do I pass variables between controllers with a service?
I want to pass a variable between controllers with a service. Here is the
service to pass the variable (in this case a song id):
'use strict';
angular.module('PortalApp')
.service('ActionbarService', ['$window', function
ActionbarService(window) {
var song;
return {
getSong: function () {
return song;
},
setSong: function(value) {
song = value;
}
};
}]);
Here is the relevant part of the controller I am setting the song from:
$scope.actionbarSetSong = function(song_id) {
ActionbarService.setSong(song_id);
}
and here is the service I am getting the song id from:
'use strict';
angular.module('PortalApp')
.controller('ActionbarCtrl', function ($scope, $rootScope, MediaService,
ActionbarService, $location, $routeParams, AudioPlayerAPI) {
// $scope.contentTemplate = '/views/album.html';
$scope.song_id = ActionbarService.getSong();
console.log($scope);
$scope.openPlaylistModal = function (song_id) {
$("#addToPlaylist").modal('show');
}
});
It is being set in the service because when I do getSong in my view it
worked (I forget where), but then when I try to set it in my 2nd
controller it doesn't work. Here's the relevant part of my view for
reference:
<div class="cell action-bar" ng-click="actionbarSetSong(song.id);"
ng-repeat="song in media.data">
<div class="offset-container pull-left">
<i ng-class="{'icon':true,'play-icon-fav':true,'on':song.favorite}"
data-ng-click="toggleFaves(song);"></i>
<i class="play-icon play-icon-play"
data-ng-click="player.setSong(song.id);" data-ng-class="{'active':
(song.id == currentSong.id && isPlaying == true), 'paused': (song.id
== currentSong.id && isPaused == true)}"></i>
</div>
No comments:
Post a Comment