我只是试图解决像这样的工厂的承诺:
.config(['$routeProvider', '$locationProvider', '$httpProvider', 'SoundRTCProvider', function ($routeProvider, $locationProvider, $httpProvider, SoundRTCProvider) {
$routeProvider
.when('/',{
templateUrl:'views/home/index.html',
controller:'Home',
resolve: {
load: function (configFactory) {
return configFactory.loadAll();
}
}
});
});
然后:
.factory('configFactory',['$rootScope', '$q', '$location', '$anchorScroll', '$routeParams', 'ngDialog', 'cacheService', 'sessionFactory', 'geoFactory', 'instrumentsFactory', 'genresFactory', 'langFactory', 'usersFactory', 'contactsFactory', function ($rootScope, $q, $location, $anchorScroll, $routeParams, ngDialog, cacheService, sessionFactory, geoFactory, instrumentsFactory, genresFactory, langFactory, usersFactory, contactsFactory) {
var initConfig = function () {
var deferred = $q.defer();
/*CONFIG PARAMS*/
$rootScope.config = {};
$rootScope.config.appWs = '';
$rootScope.config.appName = 'Dunno';
deferred.resolve('go');
return deferred.promise();
};
var initUserSession = function () {
var deferred = $q.defer();
/*----- INIT USER SESSION ---*/
$rootScope.session = {};
/*RELOAD SESSION if logged*/
if(sessionFactory.get('idUser')){
usersFactory.getMyProfile().then(function(results){
sessionFactory.initSession(results.data);
deferred.resolve();
});
}
return deferred.promise();
};
var initGravatar = function () {
var deferred = $q.defer();
/*------- INIT GRAVATARS ------*/
$rootScope.gravatar = {};
deferred.resolve();
return deferred.promise();
};
var initLang = function () {
var deferred = $q.defer();
/*------LANGUAGE---------*/
$rootScope.userLang = 'en_EN';
$rootScope.lang = {};//get key and value from here in views
//If user lang doesn't exists yet
if(cacheService.isExpired('appLang')) {
langFactory.getAll($rootScope.userLang)
.then(function (response) {
if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {
cacheService.put('appLang',response.data,$rootScope.config.minChacheTime);
$rootScope.lang = cacheService.get('appLang');
} else {
$rootScope.lang = cacheService.get('appLang');
}
deferred.resolve();
});
} else {
deferred.resolve();
$rootScope.lang = cacheService.get('appLang');
}
return deferred.promise();
};
var initGenres = function () {
var deferred = $q.defer();
/*-------GENRES-------*/
if(cacheService.isExpired('appGenres')) {
genresFactory.getAll()
.then(function (response) {
if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {
cacheService.put('appGenres',response.data,$rootScope.config.minChacheTime);
$rootScope.config.appGenres = cacheService.get('appGenres');
} else {
$rootScope.config.appGenres = cacheService.get('appGenres');
}
deferred.resolve();
});
} else {
deferred.resolve();
$rootScope.config.appGenres = cacheService.get('appGenres');
}
return deferred.promise();
};
var initInstruments = function () {
var deferred = $q.defer();
/*------INSTRUMMENTS------*/
if(cacheService.isExpired('appInstruments')) {
instrumentsFactory.getAll()
.then(function (response) {
if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {
cacheService.put('appInstruments',response.data,$rootScope.config.minChacheTime);
$rootScope.config.appInstruments = cacheService.get('appInstruments');
} else {
$rootScope.config.appInstruments = cacheService.get('appInstruments');
}
deferred.resolve();
});
} else {
deferred.resolve();
$rootScope.config.appInstruments = cacheService.get('appInstruments');
}
return deferred.promise();
};
var initGeo = function () {
var deferred = $q.defer();
/*-------GEO----------*/
if(cacheService.isExpired('appGeo')) {
geoFactory.getAll()
.then(function (response) {
if(angular.isObject(response.data) && Object.keys(response.data).length > 0) {
cacheService.put('appGeo',response.data,$rootScope.config.minChacheTime);
$rootScope.config.appGeo = cacheService.get('appGeo');
} else {
$rootScope.config.appGeo = cacheService.get('appGeo');
}
deferred.resolve();
});
} else {
deferred.resolve();
$rootScope.config.appGeo = cacheService.get('appGeo');
}
return deferred.promise();
};
var initUserContacts = function () {
var deferred = $q.defer();
/*CONTACTS*/
$rootScope.contacts = {
approved: [],
pending: []
};
if(sessionFactory.get('idUser')){
contactsFactory.getMine().then(function (res) {
if(angular.isArray(res.data) && res.data.length > 0) {
for(var i in res.data) {
//set all my pending contacts
if(res.data[i].approved !== 1) {
$rootScope.contacts.pending.push(res.data[i].idContact);
} else {
//set all my contacts
$rootScope.contacts.approved.push(res.data[i].idContact);
}
}
}
deferred.resolve();
});
} else {
deferred.resolve();
}
return deferred.promise();
};
var initLayout = function () {
var deferred = $q.defer();
/*LAYOUT*/
$rootScope.layout = {
loading:true,
userMenu:false
};
deferred.resolve();
return deferred.promise();
};
var initScroll = function () {
var deferred = $q.defer();
//Make app scroll to top by default while navigating or use #anchor in url like http://sitess.com/someroute/?scrollTo=<:idelement:> to make it scroll to that element :id:
$location.hash($routeParams.scrollTo);
$anchorScroll();
deferred.resolve();
return deferred.promise();
};
var initDialog = function () {
var deferred = $q.defer();
//Close any dialog
ngDialog.close();
deferred.resolve();
return deferred.promise();
};
var loadAll = function () {
var deferred = $q.defer();
initConfig()
.then(function(){
return initUserSession();
}).then(function () {
return initLang();
}).then(function () {
return initGenres();
}).then(function () {
return initGeo();
}).then(function () {
return initInstruments();
}).then(function () {
return initUserContacts();
}).then(function () {
return initGravatar();
}).then(function () {
return initLayout();
}).then(function () {
return initScroll();
}).then(function () {
return initDialog();
}).then(function () {
$rootScope.$on('loading:end', function(){
$rootScope.layout.loading = false;
});
$rootScope.$on('loading:progressing', function (){
$rootScope.layout.loading = true;
});
$rootScope.$on('$locationChangeError', function () {
//hide loading gif
$rootScope.layout.loading = false;
//hide loading gif
$rootScope.layout.userMenu = false;
});
//all done with configs
deferred.resolve();
});
return deferred.promise();
};
return {
initConfig:initConfig,
initUserSession:initUserSession,
initLang:initLang,
initGenres:initGenres,
initGeo:initGeo,
initGravatar:initGravatar,
initInstruments:initInstruments,
initUserContacts:initUserContacts,
initLayout:initLayout,
initScroll:initScroll,
initDialog:initDialog,
loadAll:loadAll
};
}]);
为什么我在控制台中遇到错误: TypeError:object不是函数 在initConfig(assets / js / factories.js:35:23)
这意味着第一个返回deferred.promise(); 正在生成错误,我无法理解什么是错的,感谢任何帮助
答案 0 :(得分:1)
您正在尝试将普通对象作为函数执行(这是错误的含义)。它应该是:return deferred.promise;
而不是:return deferred.promise();
。