[AngularJS] [Ionic]让Controller等待使用另一个$ http服务的服务

时间:2015-09-20 11:06:29

标签: angularjs promise ionic

问题是Controller不会等待使用另一个$ http服务的Service,而且我没有数据。

services.js:

angular.module('starter.services', [])
    .factory('Likes', function ($http) {
        var likes;
        $http.get("http://localhost:8100/js/offers.json")
                .success(function (results) {
                    likes = results.offers;
                    console.log("ok!");
                })
                .error(function (results) {
                    console.log("error");
                });
        return {
            all: function () {
                return likes;
            },
            get: function (likeId) {
                for (var i = 0; i < likes.length; i++) {
                    if (likes[i].id === parseInt(likeId)) {
                        return likes[i];
                    }
                }
                return null;
            }
        };

    })

controllers.js(部分):

.controller('OneCtrl', function($scope, $stateParams, Likes) {
  $scope.offer = Likes.get($stateParams.likedId);
})

.controller('LikedCtrl', function($scope, $stateParams, $http, Likes) {
  $scope.liked = Likes.all();
})

我已经阅读了有关promises,async(),callbacks和$ q的文章和主题,但我不知道如何在我的代码中集成这些东西,尽管经过多次尝试。

1 个答案:

答案 0 :(得分:3)

返回您服务的承诺:

print()

在控制器中

public static int GetIso8601WeekNumber(this DateTime date)
{    var thursday = date.AddDays(3 - ((int)date.DayOfWeek + 6) % 7);
     return 1 + (thursday.DayOfYear - 1) / 7;
}