路线更改时内容不会更新

时间:2014-05-04 15:23:46

标签: javascript css angularjs html5 url-routing

当我更改路线时,从/set/1更改为/set/2,然后它仍会显示来自/set/1的信息,直到我手动刷新页面为止,我已尝试添加{{ 1}}到这些页面链接的$route.refresh,但这也不起作用。有什么想法吗?

下面是路由代码,这种方法很好,所有路由都是通过链接完成的,只有ng-click个标记符合路径。

<a>

下面是SetListController的代码,它使用routeParams从服务中获取正确的信息,当我转到angular.module('magicApp', ['ngRoute']).config(['$routeProvider', function($routeProvider) { $routeProvider.when('/', { templateUrl: 'pages/home.html' }).when('/set', { redirectTo: '/sets' }).when('/set/:setID', { controller: 'SetInformationController', templateUrl: 'pages/set.html' }).when('/card', { redirectTo: '/cards' }).when('/card/:cardID', { controller: 'CardInformationController', templateUrl: 'pages/card.html' }).when('/sets', { controller: 'SetListController', templateUrl: 'pages/sets.html' }).when('/cards', { controller: 'CardListController', templateUrl: 'pages/cards.html' }).when('/search/:searchterm', { controller: 'SearchController', templateUrl: 'pages/search.html' }).otherwise({ redirectTo: '/' }); }]); 然后它返回正确的信息时,如果我再返回然后去到/set/1它仍会显示第1组中的信息,直到我刷新页面。

/set/2

HTML本身没有引用控制器或类似的东西,只是范围内的对象,即.controller('SetInformationController', function($scope, $routeParams, $route, SetInformationService, CardSetInformationService) { $scope.set = []; $scope.cards = []; function init() { SetInformationService.async($routeParams.setID).then(function(d) { $scope.set = d; }); CardSetInformationService.async($routeParams.setID).then(function(d) { $scope.cards = d; }) } init(); }) set

1 个答案:

答案 0 :(得分:0)

我明白了!问题实际上并不是我的服务路由,这里是以前的服务:

.factory('SetInformationService', function($http) {
  var promise;
  var SetInformationService = {
    async: function(id) {
      if ( !promise ) {
          // $http returns a promise, which has a then function, which also returns a promise
          promise = $http.get('http://api.mtgdb.info/sets/' + id).then(function (response) {
           // The then function here is an opportunity to modify the response
           console.log("Set Information");
           console.log(response);
           // The return value gets picked up by the then in the controller.
           return response.data;
           });
      }
      // Return the promise to the controller
      return promise;
    }
  };
  return SetInformationService;
})

应该是:

.factory('SetInformationService', function($http) {
  var promise;
  var SetInformationService = {
    async: function(id) {
      // $http returns a promise, which has a then function, which also returns a promise
      promise = $http.get('http://api.mtgdb.info/sets/' + id).then(function (response) {
        // The then function here is an opportunity to modify the response
        console.log("Set Information");
        console.log(response);
        // The return value gets picked up by the then in the controller.
        return response.data;
      });
      // Return the promise to the controller
      return promise;
    }
  };
  return SetInformationService;
})