Angular JS에서 서비스 방법의 결과를 얻는 방법은 무엇입니까?
-
20-12-2019 - |
문제
이 코드가 있습니다 :
PostApp.factory('loadPage', function ($http) {
return {
loadOtherPage: function (page, status, permition, order, cultureId) {
$http({
url: '/Administrator/Post/PagedIndex',
method: "POST",
data: { page: page, status: status, permition: permition, order: order, cultureId: cultureId }
}).success(function(data) {
return data;
});
}
};
.
});
PostApp.controller('PostController',
function ($scope, $http, loadPage) {
$scope.status = 'Published';
$scope.permition = 'Global';
$scope.order = 'Asscending';
$scope.cultureId = 1;
$scope.ListOfItems = [];
$scope.start = 2;
$scope.skip = 10;
$scope.loaddata = function () {
$scope.ListOfItems = loadPage.loadOtherPage($scope.start, $scope.status, $scope.permition, $scope.order, $scope.cultureId);
};
}
.
);
그러나 LoadPage.LoadotHotPage 서비스의 응답을 $ scope.listofitems 변수로 설정하지 마십시오. 응답은 브라우저의 콘솔에서 JSON입니다.
[{"PId":15,"Id":15,"Status":"انتشار","Permition":"سراسری","PublishedDateEn":"08/19/2013","Title":"xxxxxxxxxxxx","CultureId":1,"Username":"naser","CommentCount":0},{"PId":16,"Id":16,"Status":"انتشار","Permition":"سراسری","PublishedDateEn":"08/19/2013","Title":"yyyyyyyyyyyyyyyyyy","CultureId":1,"Username":"naser","CommentCount":0},{"PId":17,"Id":17,"Status":"انتشار","Permition":"سراسری","PublishedDateEn":"08/21/2013","Title":"zzzzzzzzzzzzzzzz","CultureId":1,"Username":"naser","CommentCount":0}]
.
최종 $ scope.listofitems가 비어 있습니까?
해결책
(편집 : 추가 선명도 변수 추가)
loadOtherPage
함수는 아무 것도 반환하지 않으므로 $scope.ListOfItems
가 비어있는 이유입니다.그것을하는 적절한 방법은 다음과 같습니다 :
loadOtherPage: function (page, status, permition, order, cultureId) {
var httpPromise = $http({
url: '/Administrator/Post/PagedIndex',
method: "POST",
data: { ... }
});
return httpPromise;
}
.
기본적으로 $http
가 발신자에게 되돌아가는 약속을 반환합니다.컨트롤러가 다음이되어야합니다 :
$scope.loaddata = function () {
var loadPagePromise = loadPage.loadOtherPage( ... );
loadPagePromise.success(function(data) {
$scope.ListOfItems = data;
});
};
. 제휴하지 않습니다 StackOverflow