1

I'm fairly new to angular and I'm trying to understand how to query from a REST API using a scope variable to determine the URI that is being pulled in the get request.

Lets say I'm in my app.controller and it has a service that spits out an array of numbers.. and for the sake of making the code minimal, I'll skip to the important part:

$scope.currentCompanyId = '0001';

$http.get('/api/'+ $scope.currentCompanyId +'/c').
  success(function(data, status, headers, config) {
    $scope.cData = data;
  }).
  error(function(data, status, headers, config) {
    // log error
  });

I know this is cheating because the $http.get is in the controller. I know it needs to be a factory of some kind.. but I have no idea how to pass the $scope.currentCompanyID to the get request and have it return the JSON. Furthermore, if $scope.currentCompanyID were to change to another number, say... '0002'.. how would the $scope.cData change to reflect the new query?

5 Answers 5

3

I don't think using $http in your controller is cheating - one reason for putting it into a factory/service is make it reusable. If you are only doing it in one place a service doesn't add much.

That being said, your service can return a function that takes a parameter:

app.factory("service", function($http) {
     return {
          getCompany: function(companyId) { ...make $http call and return data... }
     }
});

then in your controller:

service.getCompany($scope.currentComanyId).then(function(resp) {...})
Sign up to request clarification or add additional context in comments.

Comments

2

You should consider using Angular $resource because it handles a lot of your abstractions. Either way, if you want to make a new request based on changes in the scope variable, you can $watch it:

$scope.$watch('currentCompanyId', function() {
    if(!$scope.currentCompanyId) return;

    $http.get().success(); // replace with whatever mechanism you use to request data
});

1 Comment

This made the most sense to me and worked immediately after the first try. Thanks!
0

Your request wont launch if currentCompanyId change... You need to lauch your request manually .

otherwise, it seem to be correct

Comments

0

Did you look at $resource service? http://docs.angularjs.org/api/ngResource/service/$resource - it is rather convenient way to REST requests, and docs have quite a few examples that should suit you well

About changing $scope.currentCompanyID - it seems that you need to create watch for this case:

scope.$watch('currentCompanyID', function(newValue, oldValue) {
      // do your update here, assigning $scope.cData with the value returned
      // using your code:
      $http.get('/api/'+ $scope.currentCompanyId +'/c').
         success(function(data, status, headers, config) {
            $scope.cData = data;
        }).
      error(function(data, status, headers, config) {
         // log error
         });
      });

Comments

0

You simply need to pass the data in when calling your service. In your controller, you would need to include your service as a DI module and address it as so:

window.angular.module('myControllerModule', [])
     .controller('myController', ['$scope', 'myHTTPService', 
     function($scope, myHTTPService){
          $scope.currentCompanyId = 1;
          $scope.lookupPromise = myHTTPService.get($scope.currentCompanyId);
          $scope.lookupPromise.then(function(data){
               //things to do when the call is successful
          },function(data){
               //things to do when the call fails
          });
     }]);

In your service, you deal with that value like this:

window.angualr.module('myHTTPServiceModule', [])
     .factory('myHTTPService', '$http',
     function($http){

     function callHTTP(url){
          return $http.get('/api/' + url + '/c');
     }

     return {
          get: callHTTP
     };
});

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.