Javascript AngularJS-提供程序状态丢失,或为';她不是单身汉?
bootstrap.jsJavascript AngularJS-提供程序状态丢失,或为';她不是单身汉?,javascript,angularjs,Javascript,Angularjs,bootstrap.js angular.element(document).ready(function() { var auth = angular.injector(['auth', 'ngStorage', 'ng']).get('auth'); auth.user().then(function(user){ if (user && user.id) { angular.bootstrap(document, ['
angular.element(document).ready(function() {
var auth = angular.injector(['auth', 'ngStorage', 'ng']).get('auth');
auth.user().then(function(user){
if (user && user.id) {
angular.bootstrap(document, ['app']);
}
});
});
auth.js
module.provider('auth', [function(){
this.$get = ['$http', '$localStorage', function($http, $storage){
return {
_user: null,
user: function(){
var self = this;
return $http.get('/auth/user').then(function(response){
self._user = response.data;
console.log(self._user); // <- has the user object
return self._user;
});
},
}
}]
}]);
module.provider('auth',[function(){
此.$get=['$http','$localStorage',函数($http,$storage){
返回{
_用户:null,
用户:函数(){
var self=这个;
返回$http.get('/auth/user')。然后(函数(响应){
self.\u user=response.data;
console.log(self._user);//服务、工厂和提供商之间的差异:
提供者
语法:module.provider('providerName',function);
结果:将providerName声明为可注入参数时,将向您提供(new ProviderFunction()).$get()。在调用$get方法之前实例化构造函数-ProviderFunction是传递给module.provider的函数引用
module.run(function(auth, $state){
console.log(auth._user); // <- is null
});