簡體   English   中英

角$ q.all和signalR許諾

[英]angular $q.all and signalR promises

我從兩個月前開始使用Angular,因此如果出現重復問題,請提前抱歉。

雖然,我發現了類似的問題AngularJS Promises $ q.all和SignalR,但我不知道如何在我的代碼中使用它。

我通過signalR從服務器獲取數據,並且只要不從服務器獲取所有數據,就想顯示登錄頁面。 我嘗試用$ q.all來做到這一點,但出現錯誤。 波紋管是我的代碼和錯誤。

這是我的服務:

var menuItemshub = new Hub(‘menuItemsHub’, {
    rootPath: $rootScope.defaultRootPath,
    //server side methods
    methods: [‘getMenuItems’],
    queryParams: {
        ‘token’: $rootScope.loggedInUser.accessToken,
    },
    //handle connection error
    errorHandler: function (error) {
        console.error(error);
    }
});

var countrieshub = new Hub(‘countriesHub’, {
    rootPath: $rootScope.defaultRootPath,
    //server side methods
    methods: [‘getCountries’],
    queryParams: {
        ‘token’: $rootScope.loggedInUser.accessToken,
    },
    //handle connection error
    errorHandler: function (error) {
        console.error(error);
    }
});

var menuItemshubInitDone = function () {
    return menuItemshub.promise.done();
};

var countrieshubInitDone = function () {
    return countrieshub.promise.done();
};

var getMenuItems = function () {
    return menuItemshub.getMenuItems();
};

var getCountries = function () {
    return countrieshub.getCountries();
};

這是我的控制器

configurationService.menuItemshubInitDone().then(function () {
    configurationService.getMenuItems().then(function (response) {
        // Success
        $rootScope.menuItems = response.MenuItems;
    }, function (error) {
    });
});

configurationService.countrieshubInitDone().then(function () {
    configurationService.getCountries().then(function (response) {
        // Success
        $rootScope.countries = response.Countries;
        $rootScope.selectedAction = $rootScope.countries;
        $rootScope.setAction($rootScope.selectedAction[0]);

    }, function (error) {
    });
});

我想做類似的事情:

var all = $q.all([configurationService.getCountries(), 
    configurationService.getMenuItems()]);

all.then(function () {
    $rootScope.showLandingPage = false;
});

我收到以下錯誤SignalR:連接尚未完全初始化。 連接啟動后,請使用.start().done().start().fail()運行邏輯。 我嘗試過

$q.when([configurationService.menuItemshubInitDone()]);

然后打電話給$q.all但我再次得到相同的錯誤。

幾天以來,我一直在嘗試通過谷歌搜索找到解決方案,但我不知道該怎么做。

預先感謝您的幫助。

我發現我做錯了。 這是現在可以正常工作的代碼,以防萬一有人像我一樣被卡住:

$scope.userConfigurations = function () {

        var all = $q.all([getMenuItems, getCountries]);
        all.then(function () {
            $rootScope.showLandingPage = false;
        });

        var getMenuItems = configurationService.menuItemshubInitDone().then(function () {
            configurationService.getMenuItems().then(function (response) {
                // Success
                $rootScope.menuItems = response.MenuItems;
            }, function (error) {
            });
        });

        var getCountries = configurationService.countrieshubInitDone().then(function () {
            configurationService.getCountries().then(function (response) {
                // Success
                $rootScope.countries = response.Countries;
                $rootScope.selectedAction = $rootScope.countries;
                $rootScope.setAction($rootScope.selectedAction[0]);

            }, function (error) {
            });
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM