问题描述
限时送ChatGPT账号..出于某种原因,当我的路由中有一个动态属性并访问该页面时,我陷入了一个无限循环,该页面将不断请求自身.
For some reason when I have a dynamic property in my route and access that page I get stuck in an infinite loop where that page will continuously request itself.
.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider)
{
$locationProvider.html5Mode(true);
$routeProvider.when("/", {
templateUrl: "pages/index.html",
controller: "IndexCtrl"
}).when("/listhome", {
templateUrl: "pages/listhome.html",
controller: "ListHomeCtrl"
}).when("/profile", {
templateUrl: "pages/profile.html",
controller: "ProfileCtrl"
}).when("/newlist", {
templateUrl: "pages/newlist.html",
controller: "NewListCtrl"
}).when("/userlists/:id", {
templateUrl: "pages/userlists.html",
controller: "UserListsCtrl"
}).otherwise({
redirectTo: "/"
});
我正在查看的路由是/userlists/:id 路由.该路由的控制器是-
The route I'm looking at is the /userlists/:id route. The controller for that route is-
TopTenApp.controller("UserListsCtrl", ["$scope","$routeParams", function($scope, $routeParams)
{
console.log($routeParams);
$scope.lists = [];
}]);
当我访问/userlists/9 时,我看到-
And when I access /userlists/9 I see-
Object {id: "9"}
每 3 秒记录一次并且页面冻结.只要位置后面有正斜杠(/userslists/"而不是/userlists"),就会发生这种情况.
Being logged every 3 seconds and the page freezes. This seems to happen whenever there is a forward slash after the location ("/userslists/" instead of "/userlists").
有人知道这是什么原因吗?
Does anyone know the cause of this?
推荐答案
愚蠢的我意识到了这个问题.我想这是有道理的,但是当页面有多个目录"深时,模板 url 需要在它前面有一个正斜杠.
Silly me I realized the problem. I guess it makes sense but the template url needs to have a forward slash in front of it when the page is multiple "directories" deep.
.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider)
{
$locationProvider.html5Mode(true);
$routeProvider.when("/", {
templateUrl: "/pages/index.html",
controller: "IndexCtrl"
}).when("/listhome", {
templateUrl: "/pages/listhome.html",
controller: "ListHomeCtrl"
}).when("/profile", {
templateUrl: "/pages/profile.html",
controller: "ProfileCtrl"
}).when("/newlist", {
templateUrl: "/pages/newlist.html",
controller: "NewListCtrl"
}).when("/userlists/:id", {
templateUrl: "/pages/userlists.html",
controller: "UserListsCtrl"
}).otherwise({
redirectTo: "/"
});
}]);
希望能帮助遇到类似问题的其他人.
Hopefully that helps someone else with a similar problem.
这篇关于Angular Route 无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论