$route.routes 打印两次 URL,为什么?
$route.routes prints URLs twice ,why?
我已经创建了一个基本的应用程序来学习 angularJs 路由。
当我将 $routes.route
打印到浏览器控制台时,这是我看到的:
这是我的路线配置:
almRequirement.config(function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'home.html',
controller : 'homeController'
}).when('/addRequirement', {
templateUrl : 'addRequirement.html',
controller : 'addRequirementController'
}).when('/addModule', {
templateUrl : 'addModule.html',
controller : 'addModuleController'
}).when('/addContraint', {
templateUrl : 'addContraint.html',
controller : 'addContraintController'
}).when('/viewRequirement', {
templateUrl : 'viewRequirement.html',
controller : 'viewRequirementController'
}).when('/updateRequirement/:reqId', {
templateUrl : 'updateRequirement.html',
controller : 'updateRequirementController'
}).when('/viewParticularRequirement/:reqId', {
templateUrl : 'viewParticularRequirement.html',
controller : 'viewParticularRequirementController'
});
});
在 $routeProvider.config()
中配置的每个 URL 都会显示 twice.Can 谁能解释一下为什么会这样?
这可能会帮助我下次调试我的应用程序。
这是 Angular 中的设计。 $routeProvider.when()
将 "redirect" 添加到用户指定的路径,因此带或不带尾部斜线都指向相同的位置。
@param {string} path Route path (matched against
$location.path
). If $location.path
contains redundant
trailing slash or is missing one, the route will still match and the
$location.path
will be updated to add or drop the trailing slash to exactly match the route definition.
这是 source code for when()
证明(我的 /**** comments ****/
添加):
this.when = function(path, route) {
//copy original route object to preserve params inherited from proto chain
var routeCopy = shallowCopy(route);
if (angular.isUndefined(routeCopy.reloadOnSearch)) {
routeCopy.reloadOnSearch = true;
}
if (angular.isUndefined(routeCopy.caseInsensitiveMatch)) {
routeCopy.caseInsensitiveMatch = this.caseInsensitiveMatch;
}
/**** Add route as user specified it ****/
routes[path] = angular.extend(
routeCopy,
path && pathRegExp(path, routeCopy)
);
// create redirection for trailing slashes
if (path) {
var redirectPath = (path[path.length - 1] === '/')
? path.substr(0, path.length - 1)
: path + '/';
/**** Add route with / added or stripped ****/
routes[redirectPath] = angular.extend(
{redirectTo: path},
pathRegExp(redirectPath, routeCopy)
);
}
return this;
};
我已经创建了一个基本的应用程序来学习 angularJs 路由。
当我将 $routes.route
打印到浏览器控制台时,这是我看到的:
这是我的路线配置:
almRequirement.config(function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'home.html',
controller : 'homeController'
}).when('/addRequirement', {
templateUrl : 'addRequirement.html',
controller : 'addRequirementController'
}).when('/addModule', {
templateUrl : 'addModule.html',
controller : 'addModuleController'
}).when('/addContraint', {
templateUrl : 'addContraint.html',
controller : 'addContraintController'
}).when('/viewRequirement', {
templateUrl : 'viewRequirement.html',
controller : 'viewRequirementController'
}).when('/updateRequirement/:reqId', {
templateUrl : 'updateRequirement.html',
controller : 'updateRequirementController'
}).when('/viewParticularRequirement/:reqId', {
templateUrl : 'viewParticularRequirement.html',
controller : 'viewParticularRequirementController'
});
});
在 $routeProvider.config()
中配置的每个 URL 都会显示 twice.Can 谁能解释一下为什么会这样?
这可能会帮助我下次调试我的应用程序。
这是 Angular 中的设计。 $routeProvider.when()
将 "redirect" 添加到用户指定的路径,因此带或不带尾部斜线都指向相同的位置。
@param {string} path Route path (matched against
$location.path
). If$location.path
contains redundant trailing slash or is missing one, the route will still match and the
$location.path
will be updated to add or drop the trailing slash to exactly match the route definition.
这是 source code for when()
证明(我的 /**** comments ****/
添加):
this.when = function(path, route) {
//copy original route object to preserve params inherited from proto chain
var routeCopy = shallowCopy(route);
if (angular.isUndefined(routeCopy.reloadOnSearch)) {
routeCopy.reloadOnSearch = true;
}
if (angular.isUndefined(routeCopy.caseInsensitiveMatch)) {
routeCopy.caseInsensitiveMatch = this.caseInsensitiveMatch;
}
/**** Add route as user specified it ****/
routes[path] = angular.extend(
routeCopy,
path && pathRegExp(path, routeCopy)
);
// create redirection for trailing slashes
if (path) {
var redirectPath = (path[path.length - 1] === '/')
? path.substr(0, path.length - 1)
: path + '/';
/**** Add route with / added or stripped ****/
routes[redirectPath] = angular.extend(
{redirectTo: path},
pathRegExp(redirectPath, routeCopy)
);
}
return this;
};