超时 AngularJS 中的未定义函数

undefined function in timeout angularjs

本文关键字:未定义 函数 AngularJS 超时      更新时间:2023-09-26

我有以下控制器:

app.controller('ListeSASController', function($scope, $rootScope, $routeParams, $location, userService, RefreshSASServices, $timeout){
        this.IsUserLogged = function()
        {
            return userService.user().isLogged;
        };
        var promise = $timeout(RefreshSASServices.RafraichirSAS(), 100);
        this.getSAS = function(){
            return RefreshSASServices.getSAS();
        };
        $scope.$on('$locationChangeStart', function(){
            RefreshSASServices.ArreterLesRafraichissements();
        });
});

提供以下服务:

app.service('RefreshSASServices', function($http, userService, serverConfigService, $q, $timeout, $translate, constantsServices) {
    var listeSAS = [];
    var $this = this;
    var promiseRefreshSAS;
    // Getters
    this.getSAS = function()
    {
        return listeSAS;
    };

    //Setters
    this.clearDatas = function()
    {
        listeSAS = [];
    };

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


    // Fonctions de rafraichissement
    $this.ArreterLesRafraichissements = function()
    {
        if(promiseRefreshSAS !== undefined)
            $timeout.cancel(promiseRefreshSAS);
    };
    $this.GetSASFromServer = function()
    {
        var promises;
        if(userService.user().isLogged)
        {
            var uri = $this.getServerUri() + "getAllSAS/"+userService.user().UserObject._id;
            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 "";
            });
        }else{
            promises = $q.when(!userService.user().isLogged)
        }
        return promises;    
    };
    $this.RafraichirSAS = function () {
    // functions that call
            $this.GetSASFromServer()
            .then(function(promise){
                if(promise !== undefined && promise.data !== undefined)
                {
                    listeSAS = promise.data;
                    //alert('refreshing the SAS list:' + JSON.stringify(listeSAS));
                }else listeSAS = [];
                promiseRefreshSAS = $timeout($this.RafraichirSAS, 3000);
            })
            .catch(function(error)
            {
                console.error("Error :", error);
                promiseRefreshSAS = $timeout($this.RafraichirSAS, 7000);
            });
    };


});

当我使用路由加载我的页面时:

.when('/listeSAS', {
                templateUrl : './includes/sas/liste_sas.html',
                controller  : 'ListeSASController',
                controllerAs : 'controller'
            })

一切正常,如果我的数据在服务器上发生变化,它会在 UI 上更新,我的 UI 也会显示我想要的内容。一切都很好,除了当页面加载时我收到以下错误:

TypeError: undefined is not a function
    at file:///includes/libs/angular.js:14305:28
    at completeOutstandingRequest (file:///includes/libs/angular.js:4397:10)
    at file:////includes/libs/angular.js:4705:7 

这是 Angular 的函数"超时",第 14305 行是:

try {
          deferred.resolve(fn());
        } catch(e) {
          deferred.reject(e);
          $exceptionHandler(e);
        }
        finally {
          delete deferreds[promise.$$timeoutId];
        }

为什么角度会抛出这个异常?我做错了什么?要知道 :在我的登录页面上,我设置了 2 个超时,我不会停止,因为它们刷新了"全局"变量,例如私人消息的数量。尽管有错误,但两个超时仍然有效。

我将节点 webkit 与我的应用程序一起使用,当我打开此路由时(5-10 秒后),它可能崩溃了三分之一。

谢谢你的帮助。

是你调用 RafraichirSAS(),它返回 undefined 而不是传入函数?

例如,代替

$timeout(RefreshSASServices.RafraichirSAS(), 100);

$timeout(RefreshSASServices.RafraichirSAS, 100);