Javascript AngularJS-Can';无法让AJAX请求正常工作
我是新来的Javascript AngularJS-Can';无法让AJAX请求正常工作,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,我是新来的Angular(新的几个小时)。通过调整演示,我得到了我想要的东西。但是我无法让我的AJAX请求工作 我尝试了各种解决方案,但最终还是陷入了一个无休止的循环(我发现这就是角度的工作方式)。在另一种解决方案中,什么都不会发生 我当前的解决方案(尝试将peopleController放置在任何地方): 控制器: app.controller('MainController', ['$scope','$http', function($scope,$http) { //$http i
Angular
(新的几个小时)。通过调整演示,我得到了我想要的东西。但是我无法让我的AJAX
请求工作
我尝试了各种解决方案,但最终还是陷入了一个无休止的循环(我发现这就是角度的工作方式)。在另一种解决方案中,什么都不会发生
我当前的解决方案(尝试将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.
});
}
}]);
希望有人能帮我:)
您需要从peopleController
函数中删除$scope和$http,它们覆盖了$http&;amp$范围
您还应该提到ng controller='MainController'
,而不是ng controller='peopleController'
同时将名称peopleController
更改为getPeople
。我假设您希望函数在那里而不是控制器。匹配html和js文件中的控制器名称,如果html文件名中是“peopleController”,则控制器中是“peopleController”,而不是“MainController”
而不是换线
function peopleController($scope,$http){
到
如果在控制器函数中使用依赖项注入,而不是在包含的函数上使用依赖项注入,则包含的函数已经可以访问$something,因为它们在控制器函数的范围内您的视图
必须引用您声明的控制器,即MainController
:
<div ng-controller="MainController">
{{people}}
</div>
peopleController()
具有误导性;其目的是获取数据。我建议将其重命名为getPeople()
如果您想要一个名为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.
});
}
}]);
app.controller('MainController',['$scope','$http',函数($scope,$http){
//$http正在这个环境中工作
var scrollItems=[];
对于(var i=1;i您确定可以在另一个控制器中定义一个控制器吗?请尝试将peopleController
置于控制台中的mainController
之外。将其记录为“工作”,但数据未显示在屏幕上+我仍然收到一个错误:ReferenceError:范围未定义Hnx!工作正常!(只需删除倒数第二个),你的“我今天的英雄”;)!你的解决方案(和其他评论)有意义!我尝试过,但它给了我一个错误(ReferenceError:scope未定义),应用程序停止加载(或多或少是一个空屏幕).哪一行?请参阅detailsthnx,这很有意义,因此我更改了它。但不幸的是,我仍然得到了错误。
function peopleController(){
<div ng-controller="MainController">
{{people}}
</div>
$scope.people = [];
peopleController(); //initiate your ajax request
function peopleController(){ //remove the parameters
// 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; //scope -> $scope
}).
error(function(data, status, headers, config) {
console.log("fail");
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
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.
});
}
}]);