AngularJS 表达式不起作用

AngularJS Expression is not working

本文关键字:不起作用 表达式 AngularJS      更新时间:2023-09-26

我有一个看起来像这样的控制器:

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

还有一个服务.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: "...",
    ...
  },
  { ... }
]

问题是,我无法在我的index.html中输出title属性。我想通过以下方式做到这一点:

...
<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>
...

它只是不显示任何内容。我做错了什么?

在你的控制器中,你正在等待一个 .then,在你的 .all() 之后,但你还没有连接你的 getDeal 来像那样工作,这是你怎么做的:

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];
        }
    }
});