How to reload a controller's angularJS call from a factory initiated by another controller











up vote
1
down vote

favorite












I am working on an angularJS project, a music player which can select an album and then play from a list of that album's songs. Everything is working except for that after playing a song from an album(album A) and then selecting another album(album B), when I try to select the previous/next song from a player bar, which is separated from the list, the previous album's(album A) songs play. Clearly, something is not updating. I read here from a comment by Chev that factories run only once. So, I am thinking the problem might lie with my Songplayer controller(factory).
Heres the code:



AlbumController(the controller with the directive):



....
$rootscope.getAlbumId = $stateParams.getAlbumId;
this.albumData = Fixtures.getAlbum();
this.songPlayer = SongPlayer;
....


FixturesController(factory - serving the music files):



Fixtures.getAlbum = function(){
var chosenAlbum = $rootScope.getAlbumId
return chosenAlbum
....
};


SongPlayerController(factory - playing the music):



....
$rootScope.getAlbumId = $stateParams.getAlbumId;
var currentAlbum = Fixtures.getAlbum();
....
var getSongIndex = function(song){
return currentAlbum.songs.indexOf(song);
};


PlayerBarController:



code same as albumController


It seems the PlayerBarController is not registering the new album from the SongPlayer factory. I have researched and tried using $emit/$broadcast, $watch, various other callbacks, etc... I know the answer is everywhere I have looked but I simply do not have the angular skills yet to figure this out. So, any help by way of a simple code example would be greatly appreciated.
And fyi, I figure my use of $rootScope is also pretty poor, so if anyone has advice on that, I would be grateful. I used $rootscope and $stateparams in order to capture and register the album id and deliver it to the Fixtures controller.










share|improve this question
























  • Using $rootScope is ok for a small example like that. In a larger project you probably group relevant data/methods on a custom service, inject it, and use it the same way. $emit is good for an events based pattern.
    – sh0ber
    Nov 14 at 21:48

















up vote
1
down vote

favorite












I am working on an angularJS project, a music player which can select an album and then play from a list of that album's songs. Everything is working except for that after playing a song from an album(album A) and then selecting another album(album B), when I try to select the previous/next song from a player bar, which is separated from the list, the previous album's(album A) songs play. Clearly, something is not updating. I read here from a comment by Chev that factories run only once. So, I am thinking the problem might lie with my Songplayer controller(factory).
Heres the code:



AlbumController(the controller with the directive):



....
$rootscope.getAlbumId = $stateParams.getAlbumId;
this.albumData = Fixtures.getAlbum();
this.songPlayer = SongPlayer;
....


FixturesController(factory - serving the music files):



Fixtures.getAlbum = function(){
var chosenAlbum = $rootScope.getAlbumId
return chosenAlbum
....
};


SongPlayerController(factory - playing the music):



....
$rootScope.getAlbumId = $stateParams.getAlbumId;
var currentAlbum = Fixtures.getAlbum();
....
var getSongIndex = function(song){
return currentAlbum.songs.indexOf(song);
};


PlayerBarController:



code same as albumController


It seems the PlayerBarController is not registering the new album from the SongPlayer factory. I have researched and tried using $emit/$broadcast, $watch, various other callbacks, etc... I know the answer is everywhere I have looked but I simply do not have the angular skills yet to figure this out. So, any help by way of a simple code example would be greatly appreciated.
And fyi, I figure my use of $rootScope is also pretty poor, so if anyone has advice on that, I would be grateful. I used $rootscope and $stateparams in order to capture and register the album id and deliver it to the Fixtures controller.










share|improve this question
























  • Using $rootScope is ok for a small example like that. In a larger project you probably group relevant data/methods on a custom service, inject it, and use it the same way. $emit is good for an events based pattern.
    – sh0ber
    Nov 14 at 21:48















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am working on an angularJS project, a music player which can select an album and then play from a list of that album's songs. Everything is working except for that after playing a song from an album(album A) and then selecting another album(album B), when I try to select the previous/next song from a player bar, which is separated from the list, the previous album's(album A) songs play. Clearly, something is not updating. I read here from a comment by Chev that factories run only once. So, I am thinking the problem might lie with my Songplayer controller(factory).
Heres the code:



AlbumController(the controller with the directive):



....
$rootscope.getAlbumId = $stateParams.getAlbumId;
this.albumData = Fixtures.getAlbum();
this.songPlayer = SongPlayer;
....


FixturesController(factory - serving the music files):



Fixtures.getAlbum = function(){
var chosenAlbum = $rootScope.getAlbumId
return chosenAlbum
....
};


SongPlayerController(factory - playing the music):



....
$rootScope.getAlbumId = $stateParams.getAlbumId;
var currentAlbum = Fixtures.getAlbum();
....
var getSongIndex = function(song){
return currentAlbum.songs.indexOf(song);
};


PlayerBarController:



code same as albumController


It seems the PlayerBarController is not registering the new album from the SongPlayer factory. I have researched and tried using $emit/$broadcast, $watch, various other callbacks, etc... I know the answer is everywhere I have looked but I simply do not have the angular skills yet to figure this out. So, any help by way of a simple code example would be greatly appreciated.
And fyi, I figure my use of $rootScope is also pretty poor, so if anyone has advice on that, I would be grateful. I used $rootscope and $stateparams in order to capture and register the album id and deliver it to the Fixtures controller.










share|improve this question















I am working on an angularJS project, a music player which can select an album and then play from a list of that album's songs. Everything is working except for that after playing a song from an album(album A) and then selecting another album(album B), when I try to select the previous/next song from a player bar, which is separated from the list, the previous album's(album A) songs play. Clearly, something is not updating. I read here from a comment by Chev that factories run only once. So, I am thinking the problem might lie with my Songplayer controller(factory).
Heres the code:



AlbumController(the controller with the directive):



....
$rootscope.getAlbumId = $stateParams.getAlbumId;
this.albumData = Fixtures.getAlbum();
this.songPlayer = SongPlayer;
....


FixturesController(factory - serving the music files):



Fixtures.getAlbum = function(){
var chosenAlbum = $rootScope.getAlbumId
return chosenAlbum
....
};


SongPlayerController(factory - playing the music):



....
$rootScope.getAlbumId = $stateParams.getAlbumId;
var currentAlbum = Fixtures.getAlbum();
....
var getSongIndex = function(song){
return currentAlbum.songs.indexOf(song);
};


PlayerBarController:



code same as albumController


It seems the PlayerBarController is not registering the new album from the SongPlayer factory. I have researched and tried using $emit/$broadcast, $watch, various other callbacks, etc... I know the answer is everywhere I have looked but I simply do not have the angular skills yet to figure this out. So, any help by way of a simple code example would be greatly appreciated.
And fyi, I figure my use of $rootScope is also pretty poor, so if anyone has advice on that, I would be grateful. I used $rootscope and $stateparams in order to capture and register the album id and deliver it to the Fixtures controller.







angularjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 1:17

























asked Nov 14 at 21:42









brockcp

83




83












  • Using $rootScope is ok for a small example like that. In a larger project you probably group relevant data/methods on a custom service, inject it, and use it the same way. $emit is good for an events based pattern.
    – sh0ber
    Nov 14 at 21:48




















  • Using $rootScope is ok for a small example like that. In a larger project you probably group relevant data/methods on a custom service, inject it, and use it the same way. $emit is good for an events based pattern.
    – sh0ber
    Nov 14 at 21:48


















Using $rootScope is ok for a small example like that. In a larger project you probably group relevant data/methods on a custom service, inject it, and use it the same way. $emit is good for an events based pattern.
– sh0ber
Nov 14 at 21:48






Using $rootScope is ok for a small example like that. In a larger project you probably group relevant data/methods on a custom service, inject it, and use it the same way. $emit is good for an events based pattern.
– sh0ber
Nov 14 at 21:48














2 Answers
2






active

oldest

votes

















up vote
0
down vote













I think the problem from 'this.albumData = Fixtures.getAlbum();'
You can try:



let albumData = Fixtures.getAlbum();
this.albumData.length = 0;
albumData.map(item => this.albumData.push(item));





share|improve this answer




























    up vote
    0
    down vote



    accepted










    I was able to get the PlayerBar controller to register the new album by using $watch. I simply wrapped the call I already had in a $watch function, and voila!



    SongPlayer controller:



        $rootScope.$watch('getAlbumId', function(album){
    currentAlbum = Fixtures.getAlbum();
    });





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309147%2fhow-to-reload-a-controllers-angularjs-call-from-a-factory-initiated-by-another%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      I think the problem from 'this.albumData = Fixtures.getAlbum();'
      You can try:



      let albumData = Fixtures.getAlbum();
      this.albumData.length = 0;
      albumData.map(item => this.albumData.push(item));





      share|improve this answer

























        up vote
        0
        down vote













        I think the problem from 'this.albumData = Fixtures.getAlbum();'
        You can try:



        let albumData = Fixtures.getAlbum();
        this.albumData.length = 0;
        albumData.map(item => this.albumData.push(item));





        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          I think the problem from 'this.albumData = Fixtures.getAlbum();'
          You can try:



          let albumData = Fixtures.getAlbum();
          this.albumData.length = 0;
          albumData.map(item => this.albumData.push(item));





          share|improve this answer












          I think the problem from 'this.albumData = Fixtures.getAlbum();'
          You can try:



          let albumData = Fixtures.getAlbum();
          this.albumData.length = 0;
          albumData.map(item => this.albumData.push(item));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 at 1:40









          incNick

          35715




          35715
























              up vote
              0
              down vote



              accepted










              I was able to get the PlayerBar controller to register the new album by using $watch. I simply wrapped the call I already had in a $watch function, and voila!



              SongPlayer controller:



                  $rootScope.$watch('getAlbumId', function(album){
              currentAlbum = Fixtures.getAlbum();
              });





              share|improve this answer

























                up vote
                0
                down vote



                accepted










                I was able to get the PlayerBar controller to register the new album by using $watch. I simply wrapped the call I already had in a $watch function, and voila!



                SongPlayer controller:



                    $rootScope.$watch('getAlbumId', function(album){
                currentAlbum = Fixtures.getAlbum();
                });





                share|improve this answer























                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  I was able to get the PlayerBar controller to register the new album by using $watch. I simply wrapped the call I already had in a $watch function, and voila!



                  SongPlayer controller:



                      $rootScope.$watch('getAlbumId', function(album){
                  currentAlbum = Fixtures.getAlbum();
                  });





                  share|improve this answer












                  I was able to get the PlayerBar controller to register the new album by using $watch. I simply wrapped the call I already had in a $watch function, and voila!



                  SongPlayer controller:



                      $rootScope.$watch('getAlbumId', function(album){
                  currentAlbum = Fixtures.getAlbum();
                  });






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 17 at 2:08









                  brockcp

                  83




                  83






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309147%2fhow-to-reload-a-controllers-angularjs-call-from-a-factory-initiated-by-another%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?