저는 Angularjs를 처음 사용하고 cachefactory를 배우려고합니다. cachefactory를 사용하여 응답 데이터를 읽는 데 문제가 있습니다. 지금은 나머지 API를 호출 한 직후 콘솔에 출력하려고합니다. 스크립트 내 제어기는 : WebService에 발Angularjs에서 응답 본문을 추출하는 방법 CacheFactory
(function() {
"use strict";
angular
.module("productManagement")
.controller("WelcomeCtrl",
[ '$scope',
'$http',
'$timeout',
"$interval",
"$cacheFactory",
WelcomeCtrl ])
function WelcomeCtrl($scope, $http, $timeout, $interval, $cacheFactory) {
var onUserComplete = function(response) {
$scope.fname = response.data.firstName;
console.log("response.firstName:"+response.data.firstName);
var $httpDefaultCache = $cacheFactory.get('$http');
var cachedData = $httpDefaultCache.get('./webapi/details');
console.log("cachedData print:"+cachedData);
var onError = function(reason) {
$scope.error = "Could not fetch the data";
};
// GET The values from Rest API
$http({
method: 'GET',
url: './webapi/details',
cache: true
})
.then(onUserComplete, onError);
}
}());
출력이다. 내가 네트워크의 출력을 볼 수 있으며, 콘솔에 인쇄 할 수 있습니다 : HTTP 몇 객체와 함께 상태 코드
{"id":"1234","firstName":"NoName","lastName":"Newbie"}
콘솔에서 캐시 된 데이터를 인쇄는 출력한다 :
*cachedData in add training:200,{"id":"1234","firstName":"NoName","lastName":"Newbie"},[object Object],OK*
내가 아니다 위의 JSON 응답을 추출 할 수 있습니다. 나는 아래 시도했지만 그들 중 누구도 작동하지 않습니다 : 나는 또한 $ httpDefaultCache.get의 전체 URL을주는 시도
//console.log("cachedData.data print:"+cachedData.data);
//console.log("cachedData.data.id print:"+cachedData.data.id);
//console.log("cachedResponse print:"+cachedData.response.id);
하지만 여전히 작동하지 않습니다.
이 문제가 해결 되었습니까? 나는 전체 코드를 복사하지 않았다. 미리 감사드립니다.
답장을 보내 주셔서 감사합니다. Andy. 나는 이것이 키 - 값 쌍을 사용하지 않고 전체 응답을 캐싱하는 것을 제외하고는 코드에서 어느 정도 수행하고 있다고 생각한다. 나는 여기서 명백한 것을 놓치고있다. – NoNameNewbie