AngularJS Load Data from Service

前端 未结 3 1717
[愿得一人]
[愿得一人] 2020-12-02 14:41

I am having a problem getting data from a service populated into my view. I have a service defined as such

app.factory(\'nukeService\', function($rootScope,          


        
相关标签:
3条回答
  • 2020-12-02 15:21

    This should be as follows. As mentioned by NickWiggill's comment, undefined will be assigned to nukeService.data if we do not return promise.

    app.factory('nukeService', function($rootScope, $http) {
        var nukeService = {};
        //Gets the list of nuclear weapons
        nukeService.getNukes = function() {
           return $http.get('nukes/nukes.json');
        };
    
        return nukeService;
    });
    
    
      function NavigationCtrl($scope, $http, nukeService){
         nukeService.getNukes().then(function(response){
    
            $scope.data = response.data;
        });
    
       }
    
    0 讨论(0)
  • 2020-12-02 15:33

    I think this should solve your problem

    app.factory('nukeService', function($rootScope, $http) {
        var nukeService = {};
    
        nukeService.data = {};
    
        //Gets the list of nuclear weapons
        nukeService.getNukes = function() {
            $http.get('nukes/nukes.json')
                .success(function(data) {
                    nukeService.data.nukes = data;
                });
    
            return nukeService.data;
        };
    
        return nukeService;
    });
    
    function NavigationCtrl($scope, $http, nukeService){
    
        $scope.data = nukeService.getNukes();
    
        //then refer to nukes list as `data.nukes`
    
    }
    

    This is a problem with object reference.

    when you calls nukeService.getNukes() you are getting a reference to a object a then your variable $scope.nukes refers that memory location.

    After the remote server call when you set nukeService.nukes = data; you are not changing the object a instead you are changing nukeService.nukes from referencing object a to object b. But your $scope.nukes does not know about this reassignment and it still points to object a.

    My solution in this case is to pass a object a with property data and then only change the data property instead of changing reference to a

    0 讨论(0)
  • 2020-12-02 15:42

    What I do is that I expose the data straight from the service, and have a method which initializes this data. What is wrong with this?

    Service:

    app.factory('nukeService', function($scope, $http) {
        var data = {};
        data.nukes = [];
    
        //Gets the list of nuclear weapons
        var getNukes = function() {
            $http.get('nukes/nukes.json').success(function(data) {
                    data.nukes = data;
            });
        };
    
        // Fill the list with actual nukes, async why not.
        getNukes();
    
        return {
            data : data
            // expose more functions or data if you want
        };
    });
    

    Controller:

    function NavigationCtrl($scope, nukeService){
         $scope.data = nukeService.data;
         //then refer to nukes list as `$scope.data.nukes`
    }
    
    0 讨论(0)
提交回复
热议问题