javascript - Pass a variable to a service with AngularJS? -


i building app track movies , info, new angular, , cant not sure how pass variable service. want url variable instead of hardcoded. whats best way it?

tmdb.service('tmdbservice', function($http, $q){     var deferred = $q.defer();      $http.get('https://api.themoviedb.org/3/movie/popular?api_key=jkhkjhkjhkjh').then(function(data){         deferred.resolve(data);             });      this.getmovies = function(){         return deferred.promise;     } });  tmdb.controller("tmdbcontroller", function($scope, tmdbservice){     var promise = tmdbservice.getmovies();     promise.then(function(data){         $scope.movies = data;         //  console.log($scope.movies);     }) });  

there no need (in case) use $q.defer() because $http returns promise. so, service code can simplified to:

tmdb.service('tmdbservice', function($http){      this.getmovies = function(){         return $http.get('https://api.themoviedb.org/3/movie/popular?api_key=jkhkjhkjhkjh');     } }); 

then, if want send parameter can this:

tmdb.service('tmdbservice', function($http){      this.getmovies = function(movieid){         return $http.get('https://api.themoviedb.org/' + movieid + '/movie/popular?api_key=jkhkjhkjhkjh');     } }); 

in controller, can send in movieid:

tmdb.controller("tmdbcontroller", function($scope, tmdbservice){      tmdbservice.getmovies(3).then(function(response){         $scope.movies = response.data;         //  console.log($scope.movies);     }) });  

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -