$ http成功函数不在Angular js中执行

时间:2015-05-15 14:03:15

标签: django angularjs

我正在尝试从我的Django本地服务器获取HTTP响应,该服务器返回JSON响应。

JSON回复:

[{"fields": {"DOB": "2015-05-08", "image": "media/jainsubh_1394437734_6.png", "text_file": "my_Txt_Files/JN2i0oq.png", "usr_name": "ankitmishra", "email": "anbishamar@gmail.com"}, "model": "my_fr_app.new_user_reg", "pk": 15}]

角度代码是:

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<ul>
  <li ng-repeat="x in items">
    {{ x.usr_name  }}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', ['$http',function($http) {
  $http.get("http://127.0.0.1:8000/members")
  .success(function (response) {
    this.items = response.data.fields;
  }]);
});
</script>

</body>
</html>

我不知道为什么它的成功回调不会执行!!虽然服务器返回响应代码200&amp; 304表示正在执行get请求。

1 个答案:

答案 0 :(得分:-1)

只是发布替代方案,为了好玩,您也可以使用Angular中的ControllerAs功能来完成此操作。

<div ng-app="myApp" ng-controller="customersCtrl as vm"> 

<ul>
  <li ng-repeat="x in vm.items">
    {{ x.usr_name  }}
  </li>
</ul>

</div>

<script>
    var app = angular.module('myApp', []);
    app.controller('customersCtrl', ['$http', '$scope',function($http, $scope) {
        var vm = this;  // this is to help you with closure issues.  Using the ControllerAs functionality, there is no need to go into $scope at all.
        $http.get("http://127.0.0.1:8000/members")
            .success(function (response) {
                vm.items = response.data.fields;
        });
    }]);
</script>

http://toddmotto.com/digging-into-angulars-controller-as-syntax/