内容不会随着路线的改变而更新

Content doesn't update on route change

本文关键字:改变 更新 着路      更新时间:2023-09-26

当我改变路由时,从说/set/1/set/2,然后它仍然显示来自/set/1的信息,直到我手动刷新页面,我尝试将$route.refresh添加到链接到这些页面的ng-click,但这也不起作用。什么好主意吗?

下面是路由代码,这工作得很好,所有的路由都是通过链接完成的,只是<a>标签指向路由。

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: '/'
    });
}]);

下面是SetListController的代码,它使用routeParams从服务中获取正确的信息,这是有效的,当我转到/set/1时,它返回正确的信息,如果我然后返回到/set/2,它仍然显示来自set 1的信息,直到我刷新页面。

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

HTML本身没有对控制器的引用,或者类似的东西,只有作用域中的对象,即setcards

我明白了!问题不在于路由,而在于我的服务,下面是之前的服务:

.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;
})