如何在角度工厂中存储来自http服务的数据

时间:2023-01-21 19:44:46

I would like to store the value from /api/login.json globally using a service, but I think I have some sort of timing issue. The console.log statement in the controller tells me that the scope.login object is undefined.

我想使用服务全局存储/api/login.json中的值,但我认为我有一些时间问题。控制器中的console.log语句告诉我scope.login对象是未定义的。

What am I missing?

我错过了什么?

Thanks!

Factory service:

myApp.factory('LoginFactory', ['$http', function($http){

    this.data;
    $http.get('/api/login.json').success(function(data) {
        this.data = data;
    });

    return {
        getData : function(){
        return this.data;
    }
  }
}]);

Controller:

myApp.controller('AccountsCtrl', ['$scope', 'Accounts', 'LoginFactory', function($scope, Accounts, LoginFactory){
  $scope.login = LoginFactory.getData();
  console.log('$scope.login: %o', $scope.login);    
  $scope.accounts = Accounts.index();

}]);

3 个解决方案

#1


9  

you should probably avoid use of the this keyword in this context. better just to declare a new variable.

你应该避免在这种情况下使用this关键字。最好只是声明一个新变量。

myApp.factory('LoginFactory', ['$http', function ($http) {
    var data;
    $http.get('/api/login.json').success(function (d) {
        data = d;
    });
    return {
        getData: function () {
            return data;
        }
    };
}]);

you will still have a race issue though, so i would also recommend either promise chaining

你仍然会遇到种族问题,所以我也建议保证链接

myApp.factory('LoginFactory', ['$http', function ($http) {
    var promise = $http.get('/api/login.json');
    return {
        getData: function (callback) {
            promise.success(callback);
        }
    };
}]);

or even a conditional get

甚至有条件的得到

myApp.factory('LoginFactory', ['$http', function ($http) {
    var data;
    return {
        getData: function (callback) {
            if(data) {
                callback(data);
            } else {
                $http.get('/api/login.json').success(function(d) {
                    callback(data = d);
                });
            }
        }
    };
}]);

The last two approaches require you to rewrite your controller though

最后两种方法要求您重写控制器

myApp.controller('AccountsCtrl', ['$scope', 'Accounts', 'LoginFactory', function($scope, Accounts, LoginFactory){
  LoginFactory.getData(function(data) {
      $scope.login = data;
      console.log('$scope.login: %o', $scope.login);    
      $scope.accounts = Accounts.index(); //this might have to go here idk
  });
}]);

#2


3  

Extending @LoganMurphy answer. Using promise and still adding callbacks is not at all desirable. A better way of writing service could be

扩展@LoganMurphy的答案。使用promise并仍然添加回调根本不可取。更好的写作服务方式可能是

myApp.factory('LoginFactory', ['$http', function ($http, $q) {
    var data;
    return {
        getData: function () {
            if(data) {
                return $q.when(data);
            } else {
                return $http.get('/api/login.json').then(function(response){
                    data = response;
                    return data;
                });
            }
        }
    };
}]);

#3


0  

You have an issue with the this keyword and also you not handling the promise from the http.get correctly

您对this关键字有疑问,也没有正确处理http.get中的promise

I would write it like this:

我会这样写:

myApp.factory('LoginFactory', ['$http', function($http){

    return {
        getData : function(){
        return $http.get('/api/login.json');
    }
  }
}]);


myApp.controller('AccountsCtrl', ['$scope', 'Accounts', 'LoginFactory', function($scope, Accounts, LoginFactory){
  $scope.login = LoginFactory.getData().success(function(data){
     console.log(data);
     console.log('$scope.login: %o', $scope.login); 
  });

}]);

#1


9  

you should probably avoid use of the this keyword in this context. better just to declare a new variable.

你应该避免在这种情况下使用this关键字。最好只是声明一个新变量。

myApp.factory('LoginFactory', ['$http', function ($http) {
    var data;
    $http.get('/api/login.json').success(function (d) {
        data = d;
    });
    return {
        getData: function () {
            return data;
        }
    };
}]);

you will still have a race issue though, so i would also recommend either promise chaining

你仍然会遇到种族问题,所以我也建议保证链接

myApp.factory('LoginFactory', ['$http', function ($http) {
    var promise = $http.get('/api/login.json');
    return {
        getData: function (callback) {
            promise.success(callback);
        }
    };
}]);

or even a conditional get

甚至有条件的得到

myApp.factory('LoginFactory', ['$http', function ($http) {
    var data;
    return {
        getData: function (callback) {
            if(data) {
                callback(data);
            } else {
                $http.get('/api/login.json').success(function(d) {
                    callback(data = d);
                });
            }
        }
    };
}]);

The last two approaches require you to rewrite your controller though

最后两种方法要求您重写控制器

myApp.controller('AccountsCtrl', ['$scope', 'Accounts', 'LoginFactory', function($scope, Accounts, LoginFactory){
  LoginFactory.getData(function(data) {
      $scope.login = data;
      console.log('$scope.login: %o', $scope.login);    
      $scope.accounts = Accounts.index(); //this might have to go here idk
  });
}]);

#2


3  

Extending @LoganMurphy answer. Using promise and still adding callbacks is not at all desirable. A better way of writing service could be

扩展@LoganMurphy的答案。使用promise并仍然添加回调根本不可取。更好的写作服务方式可能是

myApp.factory('LoginFactory', ['$http', function ($http, $q) {
    var data;
    return {
        getData: function () {
            if(data) {
                return $q.when(data);
            } else {
                return $http.get('/api/login.json').then(function(response){
                    data = response;
                    return data;
                });
            }
        }
    };
}]);

#3


0  

You have an issue with the this keyword and also you not handling the promise from the http.get correctly

您对this关键字有疑问,也没有正确处理http.get中的promise

I would write it like this:

我会这样写:

myApp.factory('LoginFactory', ['$http', function($http){

    return {
        getData : function(){
        return $http.get('/api/login.json');
    }
  }
}]);


myApp.controller('AccountsCtrl', ['$scope', 'Accounts', 'LoginFactory', function($scope, Accounts, LoginFactory){
  $scope.login = LoginFactory.getData().success(function(data){
     console.log(data);
     console.log('$scope.login: %o', $scope.login); 
  });

}]);