Javascript AngularJs上的数据刷新

Javascript AngularJs上的数据刷新,javascript,angularjs,Javascript,Angularjs,我的数据库中有一份客户名单,他们都是在不同年份加入的。我已经配置了我的api来适应这一点,并且运行良好。我用Angularjs编写的代码可以提取正确的数据,唯一的问题是因为它是同一条路径,例如/customers/:id它不会刷新数据,如果我手动刷新浏览器,它会刷新数据。任何帮助都会很好 HTML链接 <table> <tr><td><a href="#customers/2014">2014</a></td><

我的数据库中有一份客户名单,他们都是在不同年份加入的。我已经配置了我的api来适应这一点,并且运行良好。我用Angularjs编写的代码可以提取正确的数据,唯一的问题是因为它是同一条路径,例如/customers/:id它不会刷新数据,如果我手动刷新浏览器,它会刷新数据。任何帮助都会很好

HTML链接

<table>    
<tr><td><a href="#customers/2014">2014</a></td></tr>
<tr><td><a href="#customers/2013">2013</a></td></tr>
<tr><td><a href="#customers/2012">2012</a></td></tr>
</table>
客户控制器

 (function () {

var CustomersController = function ($scope, customerService, $log, $routeParams, $location, $sce) {

    var param = $routeParams.param;
    var customer = function (data) {
    $scope.Customer= data;
    };

    var errorDetails = function (serviceResp) {
        $scope.Error = "Something went wrong ??";
    };

    var refresh = function () {
        customerService.customer(param)
        .then(customer, errorDetails);
    };

    refresh();


};

app.controller("CustomersController", ["$scope", "customerService", "$log", "$routeParams", "$location", "$sce", CustomersController]);
}());
CustomerService.js

    (function () {
    var customerService = function ($http, $q, $log, $scope) {
        var cachedCustomer;

        var customer = function (param) {
            var params = param;
            console.log("this is the "+params);

            if (cachedCustomer)
                return $q.when(cachedCustomer);

            return $http.get("http://localhost:8080/api/customers/year/" + params)
                        .then(function (serviceResp) {

                            $log.info(cachedCustomer);
                            cachedCustomer = serviceResp.data;
                            $log.info(cachedCustomer);
                            return serviceResp.data;
                        });
        };

        return {
            customer: customer,

        };
    };

    var module = angular.module("CustomerModule");
    module.factory("customerService", ["$http", "$q", "$log", customerService]);
}());

在customerService.js中的这一行

                if (cachedCustomer)
                  return $q.when(cachedCustomer);

您正在检查缓存结果,然后返回该缓存结果(如果存在)。由于您在
.then
块中设置了缓存,并且从未将其删除,因此$http调用只执行一次,因为服务充当单例对象。如果您希望能够获得更新的信息,我建议您向customerService.customer()添加一个跳过缓存检查的标志。或者,在customerService中,您可以只缓存未解析的承诺本身,而不是缓存承诺的结果(数据),然后在
中收到数据后清除该承诺。然后
。这可以防止对$http进行多次调用,直到至少有1个解析/被拒绝

浏览器刷新确实正在清除缓存客户并重新请求信息。您应该指出,在更改您通过的年份参数时,需要清除您的缓存客户

您可以创建一个缓存服务,根据请求的URL存储客户信息。换句话说,您可以创建一个类似hashmap的数组,该数组将信息存储为值,并使用URL作为键。尽管当您的数据(在后端)被大量更改时,我不建议这样做

尝试使用

$route.reload();

请在您的控制器中插入$route

可能是因为:
if(cachedCustomer)返回$q.when(cachedCustomer)嘿,我现在删除了这行代码,它可以工作:)谢谢你指出问题所在:)嘿,我现在删除了这行代码,它可以工作:)谢谢你指出问题所在:)
$route.reload();