0

I have 2 modals that use the same controller. One has:

    <div id="addToPlaylist" ng-controller="PlaylistModalCtrl">
                    <select name="playlist" ng-model="playlist" ng-options="playlist.id as playlist.name|rmExt for playlist in playlists">
                    </select>

The other has:

<div id="newPlaylist" ng-controller="PlaylistModalCtrl">
<button ng-click="createPlaylist(playlistName);">Save</button>

In my controller, I have:

angular.module('MyApp')
  .controller('PlaylistModalCtrl', function ($scope) {
    $scope.playlists = [];

    $scope.updatePlaylists = function() {
      getPlaylist.then(function (response) {
        $scope.playlists = response.data.data;
        $scope.$$phase || $scope.$apply();
      });      
    }

    $scope.createPlaylist = function(playlist_name) {
      addPlaylist(playlist_name).then(function(response) {
        $("#newPlaylist").modal('hide');
      });
    }

    $scope.updatePlaylists();
  });

So one would expect that my first view would have an updated "playlists" in the dropdown, but this isn't the case. So how can I get that view to be updated?

10
  • In html I see ng-model="playlist", but in js I see $scope.playlists, typo? Commented Oct 1, 2013 at 13:37
  • getPl is a Service call to get the latest playlist. @YeLiu, I updated my code to reflect playlists Commented Oct 1, 2013 at 13:39
  • 1
    please post your code in Fiddle or Plunker Commented Oct 1, 2013 at 13:41
  • Show how you use $scope.updatePlaylists and $scope.createPlaylist Commented Oct 1, 2013 at 13:43
  • 1
    Make sure you inject getPlaylist into the controller. Commented Oct 1, 2013 at 13:53

1 Answer 1

1

You don't seem to understand how scoping works. Here is a plunkr illustrating that two different controllers have different scopes and stuff.

http://plnkr.co/edit/LqLuLvVkE9ltzcJH6XdN?p=preview

As you can clearly see, expecting two controllers to update the same variable would not work because each of them has the variable in its own isolated scope. What you can do to battle that is to implement a pubsub service that listens to some changes for some properties, use emit and broadcast angular functions, or, the best, rethink why you would need the same controller twice in your app and redesign it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.