1

I have a controller which looks like that:

app.controller('DealCtrl', function($scope, Deals) {
    Deals.all().then(function (deals) {
        $scope.deals = deals;
        console.log($scope.deals);
    });
});

And a services.js like this:

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

app.factory('Deals', function($http) {
    function getDeals() {
        return $http.get('http://www.domain.com/library/fct.get_deals.php')
        .success(function (data) {
            var deals = data;
            return deals;
        })
        .error(function(err){
      });
  }

  return {
    all: function() {
        return getDeals();
    },
    get: function(keyID) {
        //...
    }
  }
});

It gets an object from my server with the following structure:

[
  {
    id: "1",
    category: "...",
    title: "...",
    ...
  },
  { ... }
]

The problem is, I'm not able to output the title attribute in my index.html. I want to do it in the following manner:

...
<ion-content ng-repeat="deal in deals">
  <div class="list card">

    <div class="item item-avatar">
      <img src="img.jpg">
      <h2>{{deal.title}}</h2>
      <p>Text</p>
    </div>

  </div>
</ion-content>
...

It just displays nothing. What am I doing wrong?

1 Answer 1

1

In your controller you're waiting for a .then after your .all() but you haven't wired up your getDeals to work like that, here's how you would do it:

app.factory('Deals', function($http, $q) {
    function getDeals() {
        var myPromise = $q.defer();
        $http.get('http://www.domain.com/library/fct.get_deals.php')
            .success(function (data) {
                var deals = data;
                myPromise.resolve(deals);
            })
            .error(function(err){
                myPrmoise.reject();
            });
        return myPromise.promise;
    }

    return {
        all: function() {
            return getDeals();
        },
        get: function(keyID) {
            return restaurants[keyID];
        }
    }
});
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.