I'll explain what I have so far. I have two controllers which are both using a movieService I have created which fetches a list of movies. The controllers are like this:
app.controller('MovieCtrl', ['$scope', 'movieService', function($scope, movieService) {
$scope.movieService = movieService;
}]);
app.controller('NavCtrl', ['$scope', 'movieService', function($scope, movieService) {
$scope.movieService = movieService;
}]);
My Service looks like this:
app.service('movieService', ['$http', function($http) {
var movie = undefined;
var relatedMovies = undefined;
var searchTerm = undefined;
this.update = function(search) {
searchTerm = search;
fetch();
};
function fetch() {
$http.get(/* some http call that puts data into movie */);
$http.get(/* some http call that puts data into relatedMovies */);
};
}]);
And my related view code:
<input type="text" ng-model="searchTerm" ng-model-options="{debounce: 1500}" ng-change="movieService.update(searchTerm)">
<h1>{{ movieService.movie.Title }}</h1>
So on my view I have a search bar that when submitting the search term will use movieService.update(searchTerm), which is working fine because I can log the output from fetch() to console and it will show me the movie data.
Inside the movie variable there is a Title variable, so right now, I am using {{ movieService.movie.Title }} to try and display the title of the movie in the view. The problem is that the $scope.movieService data is not updated in real time, so if I run a new search within the service, the data in the view will not be updated unless I manually do it. When the data in movieService is updated, the view should be too.
So here comes my question, how would I make sure that these $scope.movieService variables in the controllers are always up-to-date with the service? Should I use $watch or something like that? What is the best way to achieve this?
Update: Added CodePen http://codepen.io/anon/pen/JdMxaZ
updateto update the movie data in the service. So in practical terms, if I run a new search in the search bar, I would like the movie title (and other information) to update in the view. If I search for "The Avengers", it would update the movie data in the service to this movie, and the controllers/view would in turn see that it has changed and update the view to show information about "The Avengers". Hope I was clear @skeggse