Angularjs 单个控制器中的多个Http请求 var nameSpace=angular.module(“MyTutorialApp”,[]); nameSpace.controller(“MainController”[“$scope”,“$http”, 函数($scope,$http) { $http.get(“../api/api.php?fxn=“+encodeURIComponent”(“getCategories”)+ “&jsn=“+encodeURIComponent({'code':'1'}”)) .成功(功能(响应) { $scope.names=响应; }); $scope.myData={}; nameSpace.controller(“MainController”[“$scope”,“$http”, $scope.myData.doClick=函数($event,name,$scope,$http,$config) { 警报(名称); var元素=名称; 控制台日志(元素); $http.get(“../api/api.php?fxn=“+encodeURIComponent”(“getSubCategories”)+ “&jsn=“+encodeURIComponent”({'code':'element'}”)) .成功(功能(响应) { $scope.subCat=响应; }); }]); } ]); 学习英语 {{list.name}
嗨,我不能发出第二个http请求,它说get属性未定义。我尝试了很长一段时间,我无法发现哪里出了问题。请帮帮我。我刚开始使用angular。 为了解释我试图实现的目标,第一个http请求调用类别列表,填充该列表,然后单击任何类别,该类别作为第二个http请求的jsn发送。它是子类别检查这个Angularjs 单个控制器中的多个Http请求 var nameSpace=angular.module(“MyTutorialApp”,[]); nameSpace.controller(“MainController”[“$scope”,“$http”, 函数($scope,$http) { $http.get(“../api/api.php?fxn=“+encodeURIComponent”(“getCategories”)+ “&jsn=“+encodeURIComponent({'code':'1'}”)) .成功(功能(响应) { $scope.names=响应; }); $scope.myData={}; nameSpace.controller(“MainController”[“$scope”,“$http”, $scope.myData.doClick=函数($event,name,$scope,$http,$config) { 警报(名称); var元素=名称; 控制台日志(元素); $http.get(“../api/api.php?fxn=“+encodeURIComponent”(“getSubCategories”)+ “&jsn=“+encodeURIComponent”({'code':'element'}”)) .成功(功能(响应) { $scope.subCat=响应; }); }]); } ]); 学习英语 {{list.name},angularjs,Angularjs,嗨,我不能发出第二个http请求,它说get属性未定义。我尝试了很长一段时间,我无法发现哪里出了问题。请帮帮我。我刚开始使用angular。 为了解释我试图实现的目标,第一个http请求调用类别列表,填充该列表,然后单击任何类别,该类别作为第二个http请求的jsn发送。它是子类别检查这个 var nameSpace = angular.module("MyTutorialApp", []); nameSpace.controller("MainController", ['$scope',
var nameSpace = angular.module("MyTutorialApp", []);
nameSpace.controller("MainController", ['$scope', '$http',
function($scope, $http)
{
$http.get("../api/api.php?fxn=" + encodeURIComponent("getCategories") +
"&jsn=" + encodeURIComponent("{'code':'1'}"))
.success(function(response)
{
$scope.names = response;
});
$scope.myData = {};
nameSpace.controller("MainController", ['$scope', '$http',
$scope.myData.doClick = function($event, name, $scope, $http,$config)
{
alert(name);
var element = name;
console.log(element);
$http.get("../api/api.php?fxn=" + encodeURIComponent("getSubCategories") +
"&jsn=" + encodeURIComponent("{'code':'element'}"))
.success(function(response)
{
$scope.subCat = response;
});
}]);
}
]);
<!DOCTYPE html>
<head>
<title>Learning AngularJS</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script src="js/maincontroller.js"></script>
</head>
<body ng-app="MyTutorialApp" >
<div ng-controller="MainController">
<table class="table">
<tr class="row1" ng-repeat="list in names.category">
<td ng-click="myData.doClick($event,list.name)">{{ list.name }}</td>
</tr>
</table>
</div>
</body>
</html>
这是与工厂中的功能进行通信的方式。如果您这样设置,它应该可以正常工作。此外,在代码中,您定义了两次控制器,这是不正确的。不使用工厂或服务进行http调用有具体原因吗?一点也不,我不知道。Angularjs手册中没有给出。我也不知道如何使用thosei会为您提供帮助,让您了解工厂/服务。现在你可以试着用$scope.myData.doClick=function($event,name,$scope,$http,$config)替换$scope.myData.doClick=function($event,name),开始时只是$scope.myData.doClick=function($event,name),但后来我迷路了,开始尝试所有可能的方法。我只是通过今天的TypeError来实现这一点:undefined不是一个函数,这是我单击Hi时所说的,代码工作正常,但元素没有在jsn中发送,这是一个基本条件,它是未定义的
// Code goes here
var nameSpace = angular.module("MyTutorialApp", []);
nameSpace.factory('factoryRefrence', ['$http', '$q',
function($http, $q) {
return {
getCategories: function() {
var deferred = $q.defer();
$http.get("../api/api.php?fxn=" + encodeURIComponent("getCategories") +
"&jsn=" + encodeURIComponent("{'code':'1'}"))
.success(function(response) {
deferred.resolve(response);
});
return deferred.promise;
},
getsubCategories: function(element) {
var deferred = $q.defer();
$http.get("../api/api.php?fxn=" + encodeURIComponent("getSubCategories") +
"&jsn=" + encodeURIComponent({
'code': element
}))
.success(function(response) {
deferred.resolve(response);
});
return deferred.promise;
}
}
}
]);
nameSpace.controller("MainController", ['$scope', '$http', 'factoryRefrence',
function($scope, $http, factoryRefrence) {
factoryRefrence.getCategories().then(function(response) {
$scope.names = response;
});
$scope.myData = {};
$scope.myData.doClick = function(event, name) {
alert(name);
var element = name;
console.log(element);
factoryRefrence.getsubCategories().then(function(response) {
$scope.subCat = response;
});
}
}
]);