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

时间:2014-07-22 10:19:29

标签: javascript angularjs

我想要实现的是一个产生lisof Metacategories的下拉列表。一旦用户选择元类别,该参数应该传递到$ scope.meta()中的函数的URI。然后(...)。但是,我传递的参数是类别; NG-模型="类别&#34 ;.此参数未发送到URI,我收到错误:

  

TypeError:无法读取属性'类别'未定义的

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>

角:

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();

2 个答案:

答案 0 :(得分:1)

我认为错误是由此引起的:

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

因为meta方法没有返回一个promise。 如果你这样做,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;
  });
};

你可以将对meta的调用更改为

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

答案 1 :(得分:0)

缺少return

$scope.metareturn承诺让您能够使用.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
});