AngularJS Load Data from Service

前端 未结 3 1730
[愿得一人]
[愿得一人] 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;
        });
    
       }
    

提交回复
热议问题