需要AngularJS注入完成事件

时间:2015-08-05 18:59:33

标签: angularjs dependency-injection

我正在AngularJS 1.2.x中定义一个服务,如下所示,我将注入DreamFactory API以用于此服务。现在,当我这样做时,我收到类似

的错误消息
TypeError: Cannot read property 'getRecords' of undefined

我解释说注射没有完全终止。所以我继续推迟调用API 1000ms来解决这个问题。但当然,这种延迟不是一个可以接受的解决方案。 不知何故,我需要一个注入完成的事件,然后我可以启动DreamFactory.api.db.getRecord()方法。

有人可以帮忙吗?

(function () {
'use strict';

angular
    .module('app')
    .service('VisitService', VisitService);

        VisitService.$inject = ['$cookieStore', '$rootScope', '$timeout', 'DreamFactory'];
        function VisitService($cookieStore, $rootScope, $timeout, DreamFactory) {

            var service = {};

            service.getVisits = getVisits;


                function getVisits(callback) {

                    //setTimeout(function(){

                        DreamFactory.api.db.getRecords({table_name: 'visitors'})
                        .then(function(result) {

                          console.log(result.data.record);
                          callback(result.data.record)
                         },


                         function(reject) {


                         });
                    //}, 1000);


                }

                return service;


        }

})();

在控制器中,我这样称呼服务

VisitService.getVisits(function(result){ 
        alert("this callback was triggered")
        console.log(result)
    });

1 个答案:

答案 0 :(得分:1)

DreamFactory除了是基于承诺的API之外,还使用另一个必须要查找的条件。

我这样做:

$scope.$on('api:ready', function (e) {  }

这位观察员在场解决了我的问题。 谢谢所有试图帮助的人

相关问题