2
我想通过它的id获取特定的类别。我可以从剩余呼叫中获取所有类别数据。但我无法通过id获取数据。当我点击特定类别时显示null.here是我的代码。通过id使用restjax调用angularjs中的数据
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')
}
}
})();
它仍然不起作用。我无法找到代码 – sharvari
中的问题,你可以添加console.log(数据);在将数据分配到$ scope.cur_category之前? – mtamma
在控制台中显示空id参数。 – sharvari