1

I have a factory returning an object calling an internal resource containing JSON as so:

.factory('cardFactory', function ($q, $http) {
  return {
    getOtherStuff: function () {
      var deferred = $q.defer(),
        httpPromise = $http.get('/static/cards.json');

      httpPromise.then(function (response) {
        deferred.resolve(response);
      }, function (error) {
        console.error(error);
      });

      return deferred.promise;
    }
  };
});

In my controller I call it like this:

 var cardSuccess = function(data, status, headers, config) {
    $scope.cards = data.data;
  };

  cardFactory.getOtherStuff()
    .then(cardSuccess, cardError);

In the browser $scope.cards in populated but on the device it doesn't populate.

Any ideas why?

3 Answers 3

1

Hmm.., not sure.

I have it in a different way in my Ionic app, working great.

.factory('cardFactory', function ( $http ) {
    var promise;
      var cards = {
        getOtherStuff: function() {
          if ( !promise ) {
            // $http returns a promise, which has a then function, which also returns a promise
            promise = $http.get( '/static/cards.json' ).then(function (response) {
              // The then function here is an opportunity to modify the response
              // The return value gets picked up by the then in the controller.
              return response.data;
            });
          }
          return promise; // Return the promise to the controller
        }
      };
    return cards;
 })

Then, on the controller, calling it by:

$scope.getData = function() {

  // Call the async method and then do stuff with what is returned inside our own then function
  cardFactory.getOtherStuff().then(function(d) {
    $scope.cards= d;
  });
}

$scope.getData();

Hope it helps.

[EDIT:] Could it be the $http.get url, being relative? Have you tried with an absolute url?

Sign up to request clarification or add additional context in comments.

2 Comments

As it works in the browser i presume its not a url prob, ill try this and let you know, thanks!
As much "promise" as that showed unfortunately it has the same issue. I think we both implemented basically the same thing in different ways. Still not sure what the issue is.
0

Replace $scope.cards=data.data with "$scope.cards=data"

var cardSuccess = function(data, status, headers, config) {
    $scope.cards = data;
};

4 Comments

The cards are served fine in the browser so is'nt the issue, either way I tried it and unfortunately it doesn't work
Im testing on ios but i could test on android, isnt logcat a Java function? I am writing this in javascript, not sure i can log errors with it
logcat shows all logging n events, its build in eclipse.(developer.android.com/tools/debugging/debugging-projects.html) You can try debugging yr app with weinre.(people.apache.org/~pmuellr/weinre-docs/latest/Home.html)
Ive developed in android before so im familiar with logcat, as my app is packaged by ionic i dont think i can debug it through the normal way, looking into options now
0

try removing the leading slash in your url for $http. Try using 'static/cards.json' instead of '/static/cards.json'

I actually had the same problem and this fixed it for me. Hope it helps!

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.