2016-09-23 2 views
0

В принципе, это просто, но я не могу заставить его работать.угловой: просмотр не обновляется при изменении данных в другом контроллере

Примечание: используя ионный.

у меня есть:

  • вид и контроллер для предопределенных Плейлисты

  • вид и контроллер для UserPlaylists.

  • ионный попкор для назначения песни в UserPlaylist.

Он должен работать так: Я смотрю на песни в заранее определенные списки воспроизведения. Я выбираю песню, которая будет добавлена ​​в UserPlaylist. Появится popover, позволяющая выбрать UserPlaylist для добавления песни. Я выбираю его, и песня добавляется в список UserPlaylist. Приложение остается в предопределенном представлении списка воспроизведения.

Все работает. Кроме зрения с UserPlaylist не обновляется, когда я вернуться к нему с новой песней - Мне нужно перезарядить вид UserPlaylist, а затем я могу видеть добавил новую песню ...

Я не мог используйте $ apply(), всегда ли это скажет, что он уже выполняется. Является ли ионным или угловым каким-то образом кеширование?

плейлиста HTML

<div ng-hide="loading"> 
     <h2>{{playlist.name}} &nbsp; 
     <button on-tap="addAllToUserPlaylist()" class="small add-to-playlist"></button> 
     <button on-tap="addAllToQueue()" class="small add-to-queue"></button> 
     <button on-tap="playAll()" class="small play"></button> 
     </h2> 

     <app-song can-like="true" can-add-to-queue="true" can-add-to-playlist="true" on-select="tappedSong(song)" item="song" ng-repeat="song in playlist.songs" /> 
    </div> 
    </ion-content> 

Надстройка в плейлист код директивы песни

/** 
    * AddToPlaylist button was tapped 
    */ 
    function addToPlaylist(e){ 
    e.stopPropagation(); 

    // Show add to playlist template as a popover. 
    // Note that we're passing this directive's scope 
    // as the popover's parent scope. That way the popover's 
    // controller will have access to the variables here. 
    // Also, we're putting a reference to the popover in 
    // scope.popover so we can access it later to destroy it. 
    $ionicPopover.fromTemplateUrl('templates/add.to.playlist.html',{ 
     animation: 'slide-in-up', 
     scope: scope, 
     backdropClickToClose: false 
    }).then(function(popover){ 
     scope.popover = popover; 
     popover.show(); 
    }); 
    } 

код UserPlaylist:

UserPlaylistDataSource.prototype.addSongs = function(songs, id, beginning, cbk){ 
    if(beginning){ 
    Array.prototype.unshift.apply(this.songs, songs); 
    } 
    else{ 
    Array.prototype.push.apply(this.songs, songs); 
    } 
    this.save(id, cbk); 
} 

поповер добавить код:

$scope.add = function(playlist){ 

     var toAdd; 
     var msg = 'Agregaste $1 a la lista ' + playlist.name; 
     // If the parent scope has an 'item' it is the song they want to add 
     if($scope.item){ 
     toAdd = [$scope.item]; 
     msg = msg.replace('$1', 'una canción'); 
     } 
     // If the parent scope has a 'playlist' add all of it's songs 
     else if($scope.playlist){ 
     toAdd = $scope.playlist.songs; 
     msg = msg.replace('$1', $scope.playlist.songs.length+' canciones'); 
     } 
     // If the parent scope has a 'queue' add all of it's songs 
     else if($scope.queue){ 
     toAdd = $scope.queue.songs; 
     msg = msg.replace('$1', $scope.queue.songs.length+' canciones'); 
     } 

     $scope.loading = true; 
     playlist.addSongs(toAdd, playlist.id, false, function(err){ 
     $scope.loading = false; 
     //HERE playlist has the correct number of songs! So it worked! 
     if($scope.item) $scope.unflip(); // if parent scope is a single song. 
     $scope.close(); 
     if (err) { 
      return MessageService.show(err); 
     } 
     MessageService.show(msg); 
     }); 
    }; 

поповер HTML:

<ion-scroll> 
    <h3 on-tap="newPlaylist()"><img src="/img/icon-round-add.png"/> <span>Crear Nueva Lista de Reproducción</h3> 
    <div class="spinner-container" ng-show="loading"><ion-spinner></ion-spinner></div> 
    <br/> 
    <h3 class="playlist" ng-repeat="playlist in playlists" on-tap="add(playlist)"><img ng-src="{{playlist.client_data.icon_url}}"/> <span>{{playlist.name}}</span></h3> 
    </ion-scroll> 

ответ

Смежные вопросы