1

I have created a factory to run an $http GET method. I need to add an input value to the URL pulling in the JSON but I'm having trouble passing it from the controller. I can see that the URL is being created correctly, I am just missing the "query" parameter from the form input field.

Here is my HTML block:

<input type="string" class="form-control" ng-model="getMovie.title">

Here is my factory and controller:

    var app = angular.module('app', []);
    app.factory("getMovie", ['$http',function($http){
    var obj = {};
    var url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json";
    obj.getMovieInfo = function(){ 
    return $http({
        url: url,
        method: "GET",
        params:{ 
            query: this.title, // This is the value I need
            api_key: "68094e1974e7984c256beb1653319915:3:33678189",
            callback: "JSON_CALLBACK"
      },
        headers: {
            "Content-Type" : "application/json"
        }
     }).then(function successCallback(response) {
            this.movieReviews = response.data.results; 
      }, function errorCallback(response) {
            console.log("Nothing to see here...");
      });
    }
        return obj;
    }]);

app.controller('moviesCtrl', ["$scope", "getMovie", function($scope, getMovie){
    $scope.findMovie = function(){
        getMovie.getMovieInfo().then(function(response){
            $scope.results = response;
        });
    }
}]);

Thanks!

2 Answers 2

1

You can send the title as parameter to the factory method.

<input type="string" class="form-control" ng-model="title">
var app = angular.module('app', []);

app.factory("getMovie", ['$http',function($http){
var obj = {};
var url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json";
obj.getMovieInfo = function(title){ 
return $http({
    url: url,
    method: "GET",
    params:{ 
        query: title, // This is the value I need
        api_key: "68094e1974e7984c256beb1653319915:3:33678189",
        callback: "JSON_CALLBACK"
  },
    headers: {
        "Content-Type" : "application/json"
    }
 }).then(function successCallback(response) {
        this.movieReviews = response.data.results; 
  }, function errorCallback(response) {
        console.log("Nothing to see here...");
  });
}
    return obj;
}]);

app.controller('moviesCtrl', ["$scope", "getMovie", function($scope, getMovie){
    $scope.findMovie = function() {
        getMovie.getMovieInfo($scope.title).then(function(response){
            $scope.results = response;
        });
     }
}]);
Sign up to request clarification or add additional context in comments.

4 Comments

We have a winner! I can't believe I didn't try this before but I'm thrilled it works. I will mark as solved. Thanks!
mws_souza, I wonder if you can help with a new question: My ng-repeat is no longer rendering the JSON. Looks like I need to do something with "movieReviews and $scope.results but I'm not sure exactly what that is!
I figured it out: I updated vm.movieReviews = response.data.results to return response.data.results. Thanks, all!
@wdc_dt Great! I'm glad to help you :)
1

I recommend you dont use this . If you want use controllerAs syntax , use like this . You can see more in here

https://github.com/johnpapa/angular-styleguide/tree/master/a1#controllers

  app.factory("getMovie", ['$http',function($http){  
 var  vm = this
    vm.getMovie ={};

And in ajax

return $http({
        url: url,
        method: "GET",
        params:{ 
            query: vm.getMovie, // This is the value I need
            api_key: "68094e1974e7984c256beb1653319915:3:33678189",
            callback: "JSON_CALLBACK"
      },
        headers: {
            "Content-Type" : "application/json"
        }
     }).then(function successCallback(response) {
            vm.movieReviews = response.data.results; 
      }, function errorCallback(response) {
            console.log("Nothing to see here...");
      });
    }
        return obj;
    }]);

2 Comments

Thanks so much for this explanation. I am still unable to pull in the query parameter I need from my <input> tag. It's trying but is only giving me "query=%7B%7D". Do you have a suggestion for any changes I may need to make to my controller or ng-model value?
mws_souza has answered this question, thanks for the help, Thanh!

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.