我有一个看起来像这样的控制器:
app.controller('DealCtrl', function($scope, Deals) {
Deals.all().then(function (deals) {
$scope.deals = deals;
console.log($scope.deals);
});
});
像这样的services.js:
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) {
//...
}
}
});
它从我的服务器获取一个具有以下结构的对象:
[
{
id: "1",
category: "...",
title: "...",
...
},
{ ... }
]
问题是,我无法在title
中输出index.html
属性。我想以下列方式做到这一点:
...
<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>
...
它什么也没显示。我做错了什么?
答案 0 :(得分:1)
在你的控制器中,你在.all()之后等待。然后你没有连接你的getDeals就像那样工作,这就是你要怎么做的:
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];
}
}
});