0

UPDATE: I have figured it out and solved this question. To do what I was hoping to accomplish here. Do the following:

var app = angular.module('myApp', []);

app.factory('server', ['$http', function($http) {
  return {
    getResults: function(blah) {
      var req = {};
      req.blah = blah;

      return $http({
        method: 'GET',
        url: 'blah',
        params: req
      });
    }
  }
}]);

app.controller('myCtrl', ['$scope', 'server', function($scope, server) {

  $scope.getResults = function() {
    server.getResults($scope.blah)
      .success(function(data) {
        $scope.results = data;
      }).error(function(data) {
        $scope.results = {
          "Blah": "something"
        };
      });
  };
}]);

Original post: My goal is to get the UI to do a GET request to a RESTful server, and obtain a JSON in return. So in my myApp.js, I have:

var app = angular.module('myApp', []);

app.factory('server', ['$http', function($http){
    var server = {};

    server.getResults = function(blah) {
      return $http({
        method: 'JSONP', 
        url: 'blah'
      });
    }

    return server;
}]);

app.controller('MainCtrl', ['$scope', function($scope, server){
    $scope.results = server.results;
}]);

I should clarify that the response I should get from the server is a JSON object that looks like this:

var blah = {"Blah": "something"}
2
  • What's the question? Only problem I can see is you don't have a frontend_server.results property but instead, a getResults() function that returns a promise. Commented Feb 22, 2016 at 1:13
  • @Phil the frontend server takes in the color and stop selected by the user and passes the information to the backend server for computation, the backend server then returns a string (JSON object) to the front end server which gets passed back to the UI. I can't figure out how to do this Commented Feb 22, 2016 at 3:54

1 Answer 1

1

your js should be like this:

var app = angular.module('myApp', ['ui.router']);

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider){

    $stateProvider.state('home', {
        url: '/home',
        templateUrl: '/home.html',
        controller: 'MainCtrl',
    })
    $urlRouterProvider.otherwise('home');
}]);

app.factory('frontend_server', ['$http', function($http){    
   return { 
      getResults = function(colors, stops) {
      var results = null;
      var req = {
            colors:colors,
            stops: stops
           }
      return $http({
        method: 'GET', 
        url: 'blah',
        params : req
      });
    }

  }
}]);

app.controller('MainCtrl', ['$scope','frontend_server', function($scope, frontend_server){
    $scope.colors = null;
    $scope.stops = null;
    frontend_server.getResults($scope.colors, $scope.stops).success(function(data){
             $scope.results = = data;

      }).error(function(data){
            $scope.results = = data;
       });;
}]);
Sign up to request clarification or add additional context in comments.

6 Comments

... also, once the $http promise resolves, the factory-local results variable is re-assigned but $scope.results in the controller will still be null
yes, at the beginning $scope.results is null, but $scope.results will get the value lastly!
No, it won't. I suggest you try it and see.
could you please create a plunker? i wan't to know how you try it!
Here's a demo of why your code wouldn't work ~ plnkr.co/edit/4ucrlrxKNfWw6HAu8Huw?p=preview
|

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.