[英]Parameter is not recognised by the function in .then angularjs
What I am trying to achieve is a dropdown list that produces lisof Metacategories. 我想要实现的是一个产生lisof Metacategories的下拉列表。 Once the user selects the meta category, the parameter should be passed onto the URI of the function present in $scope.meta().then(...). 一旦用户选择了元类别,则应将参数传递到$ scope.meta()。then(...)中存在的函数的URI上。 however, the parameter I am passing is category; 但是,我传递的参数是类别; ng-model="category". NG-模型= “类别”。 This parameter is not sent to the URI, I am getting an error: 此参数未发送到URI,出现错误:
TypeError: Cannot read property 'category' of undefined TypeError:无法读取未定义的属性“类别”
HTML: 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';
})
}
// the function below should get a parameter category from previous function which is selected in HTML. //下面的函数应该从HTML中选择的上一个函数中获取参数类别。 Once this happens, the parameter category should be passed on as formdata.category(below) which will get the list of categories. 一旦发生这种情况,参数类别应该作为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();
I would think that the error is caused by this: 我认为该错误是由以下原因引起的:
$scope.meta().then(function meta1($scope,$http) {
because the meta method is not returning a promise. 因为meta方法没有返回承诺。 The meta method could return a promise if you did this: 如果您执行以下操作,则meta方法可能会返回诺言:
$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;
});
};
and you could change the call to meta to this 您可以将对meta的调用更改为此
$scope.meta().then(function(data) {
...
}
Missing return
. 缺少return
。
$scope.meta
should return
a promise for you to be able to use .then()
$scope.meta
应该return
一个使您能够使用.then()
的承诺。
$scope.meta= function(){
return $http.get('http://94.125.132.253:8000/getmetas').success(function(...
...
}
$scope.meta().then(function (data) {
// do something with data
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.