使用angularjs中的rest调用通过id获取数据

时间:2016-05-17 07:14:57

标签: javascript angularjs

我想通过其ID获取特定类别。我可以从休息电话中获取所有类别数据。但是我无法通过id获取数据。当我点击特定的类别时,它显示为null。这是我的代码。

service.js:

(function() {
    angular.module('shoppingPad')
        .factory('registerService', function ($q, $http) {
            //stores json data into category array.
            var category = {};
            category.subCaegory = [];
             category.cur_category = {};
            baseUrl = 'http://localhost:4000/category';
            //return {
                //function to get all category
            category.getAllCategory= function () {
                    var deferred = $q.defer();
                    //calling json data
                    return $http.get(baseUrl).then(function (response) {
                            category = response.data;
                            deferred.resolve(category);
                            return deferred.promise;
                        },
                        function (error) {
                            deferred.reject(error);
                            return deferred.promise;
                        });

                },
               // get category by id
                category.getSubCategory=function (id) {
                    console.log('inside subcategory');
                    var deferred = $q.defer();
                    return $http.get(baseUrl +'/' + id)
                        .success(function (response) {
                            deferred.resolve(
                                category.cur_category = response);
                            return deferred.promise;
                        })
                        .error(function(error){
                            deferred.reject(error);
                            return deferred.promise;
                        })

                };

            return category;
            //}
        })
})();

controller.js:

    (function() {
    angular.module('shoppingPad').controller('registerCtrl', registerCtrl);
    function registerCtrl($scope, registerService, $stateParams, $state,$rootScope,Page) {
        console.log("in Register Ctrl");
        var categoryId = $stateParams.categoryId;
        $scope.category=null;
        $scope.cur_category = null;
        //getCategory function to get all category from rest call.
        $scope.getCategory = function () {
            //setting title for registration
            Page.setTitle('Registration');
            registerService.getAllCategory().then(function (response) {
                $scope.category = response;
                console.log($scope.category);
            })
        };
            //passing id to subCategory function to get sub category by id
            $scope.subCategory = function (id) {

                //setting title for sub-category
                Page.setTitle('Sub-Register');
                if (!id) return;
                registerService.getSubCategory(id).then(function(data) {
                    $scope.cur_category = category.cur_category;
                    $state.go('app.home.register3', {'categoryId': id })
                });

            };

        $scope.signin=function(){
            $state.go('app.login.step1')
        }

    }
})();

1 个答案:

答案 0 :(得分:0)

你错过的第29行service.js

.success(function(response) {
  deferred.resolve(response.data.cur_category);
  return deferred.promise;
})
你在第24号线上错过的东西; controller.js

 registerService.getSubCategory(id).then(function(data) {
   $scope.cur_category = data;  //previously it assign to category.cur_category