Json Angularjs:ajax结果未加载到控制器中

Json Angularjs:ajax结果未加载到控制器中,json,angularjs,Json,Angularjs,场景: 我已经创建了一个文档服务,它可以从json文件中加载所有文档 myModule.factory('DocumentService', ['$http', '$resource', function ($http,$resource) { var docArray = []; $http.get('/Scripts/json/documents.json').then(function (response) { docArray = response.data;

场景: 我已经创建了一个文档服务,它可以从json文件中加载所有文档

myModule.factory('DocumentService', ['$http', '$resource', function ($http,$resource) {

var docArray = [];        
$http.get('/Scripts/json/documents.json').then(function (response) {
    docArray = response.data;
});

_getDocuments = function () {      
     return docArray;      
}
return {
    getDocuments: _getDocuments        
}
}]);
我已经创建了一个文档控制器,它从文档服务获取值

myModule.controller('HomeController', ['$scope', 'DocumentService','$http', function ($scope, docservice, $http) {

$scope.documents = docservice.getDocuments();
}]);
和html格式

<div class="btn btn-primary col-xs-12 padding-rm-bs" data-toggle="button" ng-repeat="document in documents" ng-click="changePrimaryRightPanel(document)">
            <div class="col-xs-10 ">
                <ul class="list-inline pull-left">
                    <li>
                        <span ng-show="document.hide==true" >{{document.id}}</span>
                        <img ng-src="{{document.status |DocStatusFilter}}" width="12" height="16"></li>
                    <li>{{document.name}}</li>
                </ul>
                <ul class="list-inline pull-right">
                    <li></li>
                </ul>
            </div>
            <div class="col-xs-2 ">
                <ul class="list-inline pull-right text-right">
                    <li>{{document.size | DocSizeFilter}} </li>
                    <li><span title="{{document.lastModified}}">{{document.lastModified}} </span> </li>
                </ul>
            </div>
        </div>
问题: $scope.documents未填充结果

但若我在控制器中进行更改并在控制器中移动服务功能,那个么它就可以正常工作

$http.get('/Scripts/json/documents.json').
    then(function (response) {
        $scope.documents = response.data;
    });
我怎样才能解决这个问题。基本上,我希望我的文档服务获取、更新、删除、添加文档,并且希望我的控制器只调用文档服务的方法

myModule.controller('HomeController', ['$scope', 'DocumentService','$http', function ($scope, docservice, $http) {

$scope.documents = docservice.getDocuments();
}]);
这是一种礼仪还是我需要用承诺来污染我的控制器

有人能告诉我哪一个是处理工厂和控制器及其通信的最佳实践吗
任何帮助都将不胜感激

我猜在您填充
$scope.documents
之后,您的ajax请求将结束

您可能应该在以下代码段中使用承诺:

app.factory('myService', function($http) {
  var myService = {
    async: function() {
      // $http returns a promise, which has a then function, which also returns a promise
      var promise = $http.get('test.json').then(function (response) {
        // The then function here is an opportunity to modify the response
        console.log(response);
        // The return value gets picked up by the then in the controller.
        return response.data;
      });
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl', function( myService,$scope) {
  // Call the async method and then do stuff with what is returned inside our own then function
  myService.async().then(function(d) {
    $scope.data = d;
  });
});

一切都正常工作。@Hippo你说的一切都正常吗?在控制器初始化后很长一段时间内,你在服务中交付给
的功能就会被调用。这种方法的问题是我在处理控制器中返回的服务承诺。这就是我不想要的,基本上我正在寻找的是有一个服务,它会有ajax调用的结果,当我控制器请求数据时,它会得到数据而不是承诺。然后你可以在解析器中初始化你的数据,但这不会改变这样一个事实:如果你想让它工作,你需要发送承诺。这是一种棱角分明的方式,如果不是你的,那就用脊梁。