Javascript 内容不';运输署公布更改路线最新消息
当我改变路线时,比如说从Javascript 内容不';运输署公布更改路线最新消息,javascript,css,angularjs,html,url-routing,Javascript,Css,Angularjs,Html,Url Routing,当我改变路线时,比如说从/set/1到/set/2,然后它仍然显示/set/1中的信息,直到我手动刷新页面,我尝试将$route.refresh添加到这些页面链接的ng click,但这也不起作用。有什么想法吗 下面是路由代码,这很好用,所有路由都是通过链接完成的,只是指向路由的标签 angular.module('magicApp', ['ngRoute']).config(['$routeProvider', function($routeProvider) { $routeProvider
/set/1
到/set/2
,然后它仍然显示/set/1
中的信息,直到我手动刷新页面,我尝试将$route.refresh
添加到这些页面链接的ng click
,但这也不起作用。有什么想法吗
下面是路由代码,这很好用,所有路由都是通过链接完成的,只是指向路由的标签
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本身没有对控制器的引用,或者类似的东西,只是作用域中的对象,即
set
和cards
我发现了!实际上,问题不在于路由,而在于我的服务,这里是以前的服务:
.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;
})
请粘贴显示您的路线配置的代码,以及用于更改路线的代码。我已经用它编辑了主要帖子。您没有路线控制器
/set/:setID
/set/1
和/set/2
与您的配置相同,所以它们都会显示pages/set.html。@DavidWood:最好准备一把小提琴来演示这个问题。