2014-09-03 40 views
1

我想从一个服务器加载一些json数据与后台加载屏幕。 这里是控制文件类型错误:不能调用mehod得到未定义的angularjs

angular.module('starter.controllers', []) 

    .controller('DashCtrl', function($scope) { 
    }) 
    //loading backgroundscreen of loading 
    .controller('FriendsCtrl', function($scope, Friends, $ionicLoading) { 
     $ionicLoading.show({ 
       template: 'Loading...' 
      }); 
     //this will generate the error 
     Friends.all().then(function(data){ 

      $scope.friends=data; 
      $ionicLoading.hide(); 
     }); 
    }) 

这里是我的服务文件

angular.module('starter.services', []) 
.factory('Friends', function($http) { 
    // Might use a resource here that returns a JSON array 

    // Some fake testing data 
    var friends ; 


    return { 
    all: function() { 
     $http.get('http://www.root5solutions.com/ionictest/get.json').then(function(msg){ 
       console.log("console output"+msg.data); 
       friends=msg.data; 
       console.log("from server"+msg.data); 
      }); 
     return friends; 
    }, 
    get: function(friendId) { 
     // Simple index lookup 
     return friends[friendId]; 
    } 
    } 
}); 

我收到的控制台错误

Type Error:cannot call method get of undefined. 

请帮

回答

0

因为你的服务没有返回的承诺,但是您正在等待控制器中的延迟对象。

angular.module('starter.services', []) 
.factory('Friends', function($http) { 
    // Might use a resource here that returns a JSON array 

    // Some fake testing data 
    var friends ; 


    return { 
    all: function() { 
     return $http.get('http://www.root5solutions.com/ionictest/get.json'); 
    }, 
    get: function(friendId) { 
     // Simple index lookup 
     return friends[friendId]; 
    } 
    } 
}); 
+0

没有工作是否有任何更改控制器文件 – 2014-09-03 09:16:50

+0

你又得到同样的错误? – 2014-09-03 09:51:03

+0

这是多数民众赞成在我的问题 – 2014-09-03 16:26:17

相关问题