创建一个服务以使用Angular初始化我的应用程序

时间:2014-12-03 14:45:03

标签: javascript angularjs

我正在尝试创建一个服务来初始化我的整个应用程序。它应该首次调用我的函数“refreshDatas”,它将尝试每7秒刷新一次数据:

    app.service('dataRefreshServices', function($http, userService, serverConfigService) {
$this = this;       
var listDatas = [];
        var listMessages = [];

        // Communication with the server
        $this.getServerUri = function()
        {
            return serverConfigService.getServerUri()+"majsvc/";
        };




        // Fonctions de rafraichissement
        $this.GetDataFromServer = function()
        {
            alert('Im here');
            var uri = this.getServerUri();



var promises = {};
                /* Commenting this part the time the function calling isn't working if(userService.user().isLogged())
                {
                    promises = $http.get(uri)
                    .success(function(data, status, headers, config) {
                        // this callback will be called asynchronously
                        // when the response is available
                        return data;
                    }).
                    error(function(data, status, headers, config) {
                        // called asynchronously if an error occurs
                        // or server returns response with an error status.
                        return "";
                    });
                }*/
            return promises;    
        };

        $this.RefreshDatas = function () {
                alert('im here 1');
                $this.listDatas = $this.GetDataFromServer().then(function(promise){
                alert('refreshing the list');
                $timeout(this.RefreshDatas , 7000);
                });
            }();

    });

我的应用程序控制器:

app.controller('ApplicationController', function($scope, $rootScope, $location, userService, serverConfigService, serverConnexionServices, dataRefreshServices, InitializationServices){

        $scope.message = 'Everyone come and see how good I look! Logged :'+ userService.user().isLogged;
        this.IsUserLogged = function()
        {
            return userService.user().isLogged;
        };

        this.InitializeApplicationConfiguration = function()
        { // Start the initialization of the server's configuration
            dataRefreshServices.RefreshDatas();
        };


        this.LogOut = function()
        {
            serverConnexionServices.LogOut();
        };
        });

我的index.html主体中调用了initialize函数:

<body  data-ng-controller="ApplicationController as controller" ng-init="controller.InitializeApplicationConfiguration()"> 

当我启动我的应用程序时,我在行“this.listDatas = this.GetDataFromServer()。然后(函数(承诺){”

)获得“未定义不是函数”

我理解在初始化“GetDataFromServer”函数之前调用函数refresh,但是如何才能使其工作?也许在ng-init上初始化我的应用程序不是最好的方法吗?

谢谢

编辑在回答提示后更新了代码

1 个答案:

答案 0 :(得分:0)

'this'取决于执行上下文,可以在回调中重新定义。您应该将'this'保存在临时变量中,然后引用它。

 var $this = this;
 $this.refreshData = function(...)

此外,ng-init不是调用初始化函数的正确位置。在控制器内执行初始化代码。

app.controller('ApplicationController', function($scope, $rootScope, $location, userService, serverConfigService, serverConnexionServices, dataRefreshServices, InitializationServices){

        ...
         this.InitializeApplicationConfiguration();
   });