1

我是 Angularjs 的新手,正在尝试学习 cachefactory。我无法使用 cachefactory 读取响应数据。截至目前,我只是想在调用其余 api 后立即在控制台上打印它。我的 javascript 控制器是:

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

    }
}());

Web 服务的输出是。我可以在 Network 中看到输出,也可以在控制台上打印它:

{"id":"1234","firstName":"NoName","lastName":"Newbie"}

在控制台上打印缓存数据时,它会打印出 http 状态代码以及一些对象:

*cachedData in add training:200,{"id":"1234","firstName":"NoName","lastName":"Newbie"},[object Object],OK*

我无法从上面提取 JSON 响应。我尝试了以下方法,但没有一个有效:

//console.log("cachedData.data print:"+cachedData.data);
//console.log("cachedData.data.id print:"+cachedData.data.id);
//console.log("cachedResponse print:"+cachedData.response.id);

我也尝试在 $httpDefaultCache.get 中提供完整的 URL,但它仍然不起作用。

有什么解决办法吗?我没有复制整个代码以保持简短。提前致谢 !

4

0 回答 0