gpt4 book ai didi

angularjs - 单个 Controller 中的多个 Http 请求

转载 作者:行者123 更新时间:2023-12-03 07:58:21 24 4
gpt4 key购买 nike

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 请求,它说获取属性未定义。我尝试了很长时间,但无法发现问题出在哪里。请帮助我。我刚开始使用 Angular 。为了解释我想要实现的目标,第一个 http 请求调用类别列表,填充该列表,然后单击任何类别,该类别作为第二个 http 请求的 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;
});
}
}
]);

Demo

这是在工厂中与函数通信的方式。如果你这样设置它应该可以正常工作。除了在您的代码中,您还定义了两次 Controller ,这是不对的。

关于angularjs - 单个 Controller 中的多个 Http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25934411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com