Try*_*ing 4 angularjs angularjs-routing
我正在定义这样的路线.
$routeProvider
.when('/Home', {
name: 'Main',
templateUrl: 'Main.html',
controller: 'MainController',
controllerAs: 'ctrl'
})
.when('/About', {
name: 'About',
templateUrl: 'About.html',
controller: 'AboutController',
controllerAs: 'ctrl'
})
Run Code Online (Sandbox Code Playgroud)
当我在主控制器中时,如何在控制器中通过查询名称"关于"来查找"/关于"URL?
您可以注入$location控制器并使用$location.path()或注入URL $route来获取路由名称.
例:
function MainCntl($scope,$location,$route) {
$scope.location = $location.path(); // '/Home'
$scope.routeName= $route.current.$$route.name; //'Main'
}
Run Code Online (Sandbox Code Playgroud)
由于没有内置任何东西,路由的扩展可以很好地解决这个问题:
$provide.decorator('$route', ($delegate) => {
$delegate.getRoute = (name) => {
var result = null;
angular.forEach($delegate.routes, (config, route) => {
if (config.name === name) {
result = route;
}
});
return result;
};
return $delegate;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9716 次 |
| 最近记录: |