我是Angular的新手(几个小时)。通过调整演示,我得到了我想要的东西。但是我无法获得我的AJAX请求进行工作。

我尝试了各种解决方案,但是却陷入了无尽的循环(弄清楚了这就是Angular的工作方式)。在其他解决方案中,什么也没有发生。

我当前的解决方案(试图将peopleController放在各处):

控制器:

app.controller('MainController', ['$scope','$http', function($scope,$http) {
    //$http is working in this

    var scrollItems = [];

    for (var i=1; i<=100; i++) {
        scrollItems.push('Item ' + i);
    }

    $scope.scrollItems = scrollItems;


    function peopleController($scope,$http){
        // Simple GET request example :
        $http.get('/public/ajax.php').
        success(function(data, status, headers, config) {
            console.log("worked");
            // this callback will be called asynchronously
            // when the response is available
            scope.people = data;
            }).error(function(data, status, headers, config) {
                console.log("fail");
                // called asynchronously if an error occurs
                // or server returns response with an error status.
            });
     }
}]);


HTML:

<div ng-controller="peopleController">
     {{people}}
</div>


但这给了我这个错误:

Error: [ng:areq] http://errors.angularjs.org/1.3.0/ng/areq?p0=peopleController&p1=not%20aNaNunction%2C%20got%20undefined
    at Error (native)
    at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:6:416
    at Mb (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:19:510)
    at nb (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:20:78)
    at $get (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:74:494)
    at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:56:415
    at r (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:7:408)
    at M (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:56:281)
    at g (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:51:201)
    at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js:50:309


希望有人可以在这里帮助我:)

Other solutions i tried for example

最佳答案

如果您需要一个单独的控制器,称为peopleController,则需要按以下方式分别定义它:

app.controller('MainController', ['$scope','$http', function($scope,$http) {
      //$http is working in this

      var scrollItems = [];

      for (var i=1; i<=100; i++) {
        scrollItems.push('Item ' + i);
      }

      $scope.scrollItems = scrollItems;



 }]);

 app.controller('peopleController', ['$scope','$http', function ($scope,$http){
        // Simple GET request example :
        $http.get('/public/ajax.php').
          success(function(data, status, headers, config) {
          console.log("worked");
            // this callback will be called asynchronously
            // when the response is available
            $scope.people = data;
          }).
          error(function(data, status, headers, config) {
          console.log("fail");
            // called asynchronously if an error occurs
            // or server returns response with an error status.
          });
        }
 }]);

关于javascript - AngularJS-无法获取AJAX请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30011129/

10-09 15:08