【发布时间】:2016-02-15 05:48:30
【问题描述】:
两个月前我开始与 Angular 合作,如果我的问题重复,请提前道歉。
虽然,我发现了类似的问题 AngularJS Promises $q.all and SignalR 我不知道如何将其放入我的代码中。
我通过 signalR 从服务器获取数据,只要我没有从服务器获取所有数据,我就想显示登录页面。我尝试用 $q.all 来做到这一点,但我得到了错误。 Bellow 是我得到的代码和错误。
这是我的服务:
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,但我又遇到了同样的错误。
几天来我一直试图通过谷歌搜索找到解决方案,但我无法弄清楚我需要做什么。
提前感谢您的帮助。
【问题讨论】:
标签: javascript angularjs promise signalr