在工厂的GET请求之后,AngularJs $ scope不会更新

Joã*_*ins 9 javascript httprequest angularjs angularjs-scope

我一直在尝试AngularJS进行实验项目,我也遇到了这个问题.在我的HTML中,我想显示一个项目列表

的index.html

<h1>Some list</h1>
<div ng-controller="datlist">
    <div ng-repeat="item in items">
        <div>Item description: {{item.description}}</div>
        <div>Item name: {{item.name}}</div>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

起初我使用一个简单的控制器来获取信息并使用以下更新视图:

controllers.js(原创)

function datlist($scope,$http){
$http({method: 'GET', url: 'http://localhost:61686/getdatlist?format=json', headers: {'Access-Control-Allow-Origin': 'localhost:*'}}).
    success(function(data, status, headers, config) {
        $scope.items=data.itemsToReturn;
        console.log(data);
}).
error(function(data, status, headers, config) {
    console.log("fail");
});

}
Run Code Online (Sandbox Code Playgroud)

这工作得很好,我可以获得项目列表.同时,通过更改我的结构以使用工厂发出相同的请求并将其绑定到$ scope.items它不起作用.我尝试了很多$ watch的变种,但是我无法更新$ scope.items.我发现了一些关于$ apply的内容,但我真的无法理解如何使用它.

controllers.js(新的)

var datModule = angular.module('datModule',[]);
datModule.controller('datlist', function ($scope, datfactory){
    $scope.items = datfactory.getlist();
    $scope.$watch($scope.items, $scope.items = datfactory.getlist());
});
datModule.factory('datfactory', function ($http){
    var factory = {};
    factory.getlist = function(){
        $http({method: 'GET', url: 'http://localhost:61686/getdatlist?format=json', headers: {'Access-Control-Allow-Origin': 'localhost:*'}}).
        success(function(data, status, headers, config) {
            console.log(data.itemsToReturn); //I get the correct items, all seems ok here
            return data.itemsToReturn;
        }).
        error(function(data, status, headers, config) {
            console.log("fail");
        });

    }
    return factory;
});
Run Code Online (Sandbox Code Playgroud)

任何有关这方面的想法都会很棒.PS:我发现很多帖子都在谈论这个问题但是没有一个能帮我找到完整的解决方案.

谢谢

Kon*_*ass 18

使用手表有点难看.

试试这个:

datModule.factory('datfactory', function ($http, $q){

    this.getlist = function(){            
        return $http.get('http://localhost:61686/getdatlist?format=json',{'Access-Control-Allow-Origin': 'localhost:*'})
            .then(function(response) {
              console.log(response); //I get the correct items, all seems ok here
              return response.data.itemsToReturn;
            });            
    }
    return this;
});

datModule.controller('datlist', function ($scope, datfactory){
    datfactory.getlist()
      .then(function(arrItems){
         $scope.items = arrItems;
       });
});
Run Code Online (Sandbox Code Playgroud)

这就是你如何使用promise来处理异步问题.

更新(2015年1月15日):现在更时尚!


Dan*_*mer 8

该问题与范围摘要周期无关.您正试图直接从回调内部返回,这不是异步的.

我建议您使用promise,或直接返回http promise.

var factory = {};
factory.getlist = function(){
    return $http({method: 'GET', url: 'http://localhost:61686/getdatlist?format=json', headers: {'Access-Control-Allow-Origin': 'localhost:*'}});

}
return factory;
Run Code Online (Sandbox Code Playgroud)

直接返回承诺,并处理成功/失败 factory.getlist().success()

或者,如果要围绕请求包含其他逻辑,请使用您自己的承诺.

var datModule = angular.module('datModule',[]);

datModule.controller('datlist', function ($scope, datfactory){
    $scope.items = [];
    datfactory.getlist().then(function(data) { $scope.items = data });
});

datModule.factory('datfactory', function ($http, $q){
    var factory = {};
    factory.getlist = function(){
        var defer = $q.defer();
        $http({method: 'GET', url: 'http://localhost:61686/getdatlist?format=json', headers: {'Access-Control-Allow-Origin': 'localhost:*'}}).
        success(function(data) {
            // alter data if needed
            defer.resolve(data.itemsToReturn);
        }).
        error(function(data, status, headers, config) {
            defer.reject();
        });
        return defer.promise;
    }
    return factory;
});
Run Code Online (Sandbox Code Playgroud)