为什么我不能从远程读取数据

时间:2013-08-05 10:32:44

标签: angularjs

我是angulrJs的新人,

我试图用$ resource

读取数据

就像那样(服务代码)

angular.module('elnApp')
 .factory('programService', function ($resource, baseUrl) {
  return $resource(baseUrl + '/rest/program/:id', {}, {
    query:  {method: 'GET', isArray: true},
    get:    {method: 'GET'},
    remove: {method: 'DELETE'},
    edit:   {method: 'PUT'},
    add:    {method: 'POST'}
  });
});

(控制器代码)

angular.module('elnApp')
  .controller('GridCtrl', function ($scope, $http, $routeParams, $location,  
  programService) {
    $scope.filterOptions = {
      filterText: "",
      useExternalFilter: true
    };
   $scope.totalServerItems = 0;
   $scope.pagingOptions = {
      pageSizes: [5, 10, 20],
      pageSize: 5,
      currentPage: 1
   };  
   $scope.setPagingData = function(data, page, pageSize){   
      var pagedData = data.slice((page - 1) * pageSize, page * pageSize);;
      $scope.myData = pagedData;
      $scope.totalServerItems = data.length;
      if (!$scope.$$phase) {
          $scope.$apply();
      }
  };
  $scope.getPagedDataAsync = function (pageSize, page, searchText) {
     setTimeout(function () { 
        var data;
        if ($routeParams.id) {
           data = {'program': programService.get({id: $routeParams.id})};
        }
        if ($location.path() === '/grid') {
          data = programService.query();
        }
        $scope.setPagingData(data,page,pageSize);
    }, 100);

};

我想获取数据并显示它,

但我无法获取数据,我很困惑

请帮助

1 个答案:

答案 0 :(得分:0)

而不是这样做

data = {'program': programService.get({id: $routeParams.id})};

你需要使用回调函数来获取这样的数据

programService.get({
    id: $routeParams.id
}, function (result) {
    data = {
        'program': result
    };
});

您还需要更改data = programService.query();以使用回调函数。希望它有所帮助。