0

When I update the data in the variable $scope.userMovies, ng-repeat does not update if inside a ng-view. Outside ng-view it gets updated correctly.

What am I missing that ng-repeat does not work as I expect?

UPDATE: As requested I moved the code to Plunker to better undestand my problem: Plunker Code Example

UPDATE: Solved saving the variable userMovies in the rootScope.

3
  • Could you provide a plunker or a fiddle of this issue? Commented Apr 24, 2013 at 17:04
  • 1
    Are you initializing userMovies to anything before the getMoviesByTitle call happens? Commented Apr 24, 2013 at 17:17
  • Yes I am initializing userMovies in the controller, see updated example on plunker. Commented Apr 24, 2013 at 22:19

1 Answer 1

2

ng-view creates its own scope. What is likely happening is that you are creating $scope property userMovies on this ng-view child scope, which your MovieboxCtrl scope can not see/access. As @darkporter alluded to in a comment, your MovieboxCtrl should create an empty userMovies array. Then when the MovieCtrl sets userMovies, it will update the existing object reference, rather than create a new property.


Here is a plunker that stores the data in the service, rather than in a controller.

angular.module('moviebox.services', [])
  .factory('movieboxApi', function() {
    var model = { movies: [] };
    model.getMoviesByTitle = function(query) {
      angular.copy([{'title': 'foo'},{'title': 'foo1'},
      {'title': 'foo2'},{'title': 'foo3'},
      {'title': 'foo4'}], model.movies);
    };
    return model;
  });
app.controller('MainCtrl', function($scope, movieboxApi) {
    $scope.getMoviesByTitle = function() {
      movieboxApi.getMoviesByTitle($scope.query);
    };
    $scope.userMovies = movieboxApi.movies;
});
app.controller('MovieboxCtrl', function ($scope, movieboxApi) {
    $scope.userMovies = movieboxApi.movies;
});

HTML change:

<body ng-controller="MainCtrl">

angular.copy() is used because the controllers have a reference to the model.movies array that the service initially created. We want to modify that array, not assign a new one.

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

4 Comments

I'm already initializing userMovies in the controller. Do I have to initialize elsewhere?
@Codingmonkey, when you click the "Go" button, the getMoviesByTitle() function associated with the MovieboxCtrl on line 23 of your index.html file is being called. It populates the $scope associated with that controller. Inside ng-view, another MovieboxCtrl (and another $scope) are created. Changes you make to one scope are not visible in the other.
So if I want the ng-repeat to react to changes when clicking "go", the button have to be inside the ng-view partial, so that it uses the same controller and scope?
@Codingmonkey, I suggest you save your data in your service, then have your controllers access the data via the service. See my updated answer and plunker.

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.