gpt4 book ai didi

javascript - .then angularjs 中的函数无法识别参数

转载 作者:行者123 更新时间:2023-12-03 12:12:43 25 4
gpt4 key购买 nike

我想要实现的是一个生成 lisof 元类别的下拉列表。一旦用户选择元类别,参数应传递到 $scope.meta().then(...) 中存在的函数的 URI。但是,我传递的参数是类别; ng-model=“类别”。此参数未发送到 URI,我收到错误:

TypeError: Cannot read property 'category' of undefined

HTML:

 </select></center >
<center> <select ng-model="category" > <!-- This produces drop down list from which the users selects a category which should then get passed to the meta1() function as a parameter to produce a further list for the drop down after that-->
<option size=50 value="" selected>Select a meta category</option>
<option ng-repeat="meta in metas.metas" value="{{meta}}">{{meta}} </option>

</select></center>
<center> <select ng-model="category" >
<option size=50 value="" disabled selected>Select a category</option>
<option ng-repeat="category in categories.categories" value="{{category}}">{{category}} </option>

</select></center>

Angular :

myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/post', {
templateUrl: 'templates/post.html',
controller: 'PostCtrl'
})
}])
.controller('UserListCtrl', function ($scope, $http) {
$scope.list = function () { // this gets the list of places which works completely fine.
$http.get('http://94.125.132.253:8001/getuncategorisedplaces').success(function (data) {

$scope.places = data;
console.log(data);
$scope.message = 'List of Uncategorised places';
$scope.meta = function () { // this gets list of meta categories, which is working fine.
return $http.get('http://94.125.132.253:8000/getmetas').success(function (data) {

$scope.metas = data;
console.log(data);
$scope.message = 'List of Uncategorised places';
})
}

//下面的函数应该从 HTML 中选择的上一个函数中获取参数类别。一旦发生这种情况,参数类别应作为 formdata.category(如下)传递,它将获取类别列表。

        $scope.meta().then(function (data) {
var formdata = {
'category': this.category,
}
var inserturl = 'http://94.125.132.253:8000/getcategories?meta=' + formdata.category;
$http.get(inserturl).success(function (data) {

$scope.categories = data;
console.log(formdata.category);
console.log(data);
$scope.message = 'List of Uncategorised places';
});


});
})
}
$scope.list();

最佳答案

我认为错误是由以下原因引起的:

$scope.meta().then(function meta1($scope,$http) {

因为元方法没有返回 promise 。如果您这样做,元方法可能会返回一个 promise :

$scope.meta= function(){
return $http.get('http://94.125.132.253:8000/getmetas').success(function (data) {
$scope.metas = data;
console.log(data);
$scope.message = 'List of Uncategorised places';
return data;
});
};

您可以将对元的调用更改为此

$scope.meta().then(function(data) {
...
}

关于javascript - .then angularjs 中的函数无法识别参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24884701/

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