0

I'm currently working with Angular and have an issue with $http. A couple of my controllers use $http just file, applying the results to $scope.variable however on my current controller I'm having an issue.

app.factory('getUserData', function($http) {
  var user = [],
    url = 'res/asp/manageaccount.asp?action=',
    custid = getCookie('custid');

  return {
    getUserInfo: function() {
      url += 'getUserInfo&custid=' + custid;
      return $http.get(url);
    },
    getShipInfo: function(callback) {
      url += 'getShipInfo&custid=' + custid;
      return $http.get(url).then(function(response) {
        user = response.data;
        return user;
      });
    }
  };
});

app.controller('accountController', ['$scope', 'getUserData',
  function($scope, getUserData) {
    $scope.custid = getCookie('custid');
    getUserData.getUserInfo().then(function(data) {
      $scope.userInfo = data.data;
      console.log($scope.userInfo); // Logs the array with the object data I need
    });
    // Accessing $scope.userInfo here is 'undefined'
  }
]);

I've tried two different ways to return the data. The data returns fine however I cannot access it in my page - only within the scope of defining $scope.userInfo. Getting the data isn't the issue it is displaying it on the page.

<div class="container" ng-controller="accountController">
  
  <h1>Welcome Back, {{userInfo.fname}}!</h1> <!-- Doesn't work -->
  
  <div class="row" ng-controller="cartController">
    <!-- This works fine -->  
  </div>
  
  <div class="row">
    {{userInfo.fname}} <!-- Doesn't work -->
    {{custID}} <!-- Works -->
  </div>
  
</div>

TL;DR

$http returns data but cannot access it with {{userInfo.fname}} in my page. I've looked a lot of SO questions/answers and this one seems to be the most relevant.

6
  • what does console.log(data); output? Commented Feb 24, 2015 at 18:36
  • @Ronnie Object {data: Array[1], status: 200, headers: function, config: Object, statusText: "OK"} and data.data output [Object] which contains the data I need. Commented Feb 24, 2015 at 18:39
  • seems to be an array, what if you try access {{obj[0].prop}}? Or scope.userInfo = data.data[0] Commented Feb 24, 2015 at 18:44
  • what does console.log(data[0]); output? Commented Feb 24, 2015 at 18:45
  • {{obj.prop}} was just a placeholder. I'm actually using {{userInfo.fname}} to try and write out to the page. Logging $scope.userInfo returns the object properly but can ONLY be done while inside the getUserData.getUserInfo() declaration. Commented Feb 24, 2015 at 18:48

1 Answer 1

1

I think this is the answer you are looking for. You should rename .then(data) to .then(response) to avoid confusion. response.data[0] should be the object

app.controller('accountController', ['$scope', 'getUserData', function($scope, getUserData)
{
  $scope.custid = getCookie('custid');
  getUserData.getUserInfo().then(function(response)
  {
    $scope.userInfo = response.data[0];
  });
}]);
Sign up to request clarification or add additional context in comments.

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.