在Angular中,如何从控制器内部找到"called"控制器的路由?
In Angular, how to find the route that "called" the controller from inside the controller?
我知道两条或多条路线可以共享同一个控制器。但是,在控制器上,我想知道是哪条路由"called"而已。为什么?哪条路线将决定我是加载记录列表、通过其 ID 加载一条记录,还是根本不加载任何内容。
我有以下路线配置:
angular.module("app").config(function ($routeProvider) {
$routeProvider.when("/", {
templateUrl: "home.html"
}).when("/records", {
templateUrl: "records.html",
controller: "RecordsController"
}).when("/record/create", {
templateUrl: "editRecord.html",
controller: "RecordsController"
}).when("/template/edit/:id", {
templateUrl: "editRecord.html",
controller: "RecordsController"
}).otherwise({
redirectTo: "/"
});
});
我只想使用一个控制器,所以我有一个:
angular.module("app").controller("RecordsController", function($http, $routeParams){
/* I know that if $routeParams.id exists, it's probably the
/record/edit/:id route. But if $routeParams.id is null/undefined,
I don't know which of /records or /record/create "called"
this controller. I would like to know which route so I can
decide to load a list (if /records) or not (for /record/create).
*/
});
除非有一些技巧可以从路由配置中提取,比如 resolve,不知何故?
使用$location.path()
来自 docs:
The $location service parses the URL in the browser address bar (based
on window.location) and makes the URL available to your application.
...
Getter and setter methods
// get the current path
$location.path();
在你的控制器中使用
angular.module("app").controller("RecordsController", function($http, $routeParams, $location){
var path = $location.path();
// Do something with `path` ...
});
演示
导入 $locationProvider 并在控制器中检查 $location.path()
if($location.path() == '/record")
我知道两条或多条路线可以共享同一个控制器。但是,在控制器上,我想知道是哪条路由"called"而已。为什么?哪条路线将决定我是加载记录列表、通过其 ID 加载一条记录,还是根本不加载任何内容。
我有以下路线配置:
angular.module("app").config(function ($routeProvider) {
$routeProvider.when("/", {
templateUrl: "home.html"
}).when("/records", {
templateUrl: "records.html",
controller: "RecordsController"
}).when("/record/create", {
templateUrl: "editRecord.html",
controller: "RecordsController"
}).when("/template/edit/:id", {
templateUrl: "editRecord.html",
controller: "RecordsController"
}).otherwise({
redirectTo: "/"
});
});
我只想使用一个控制器,所以我有一个:
angular.module("app").controller("RecordsController", function($http, $routeParams){
/* I know that if $routeParams.id exists, it's probably the
/record/edit/:id route. But if $routeParams.id is null/undefined,
I don't know which of /records or /record/create "called"
this controller. I would like to know which route so I can
decide to load a list (if /records) or not (for /record/create).
*/
});
除非有一些技巧可以从路由配置中提取,比如 resolve,不知何故?
使用$location.path()
来自 docs:
The $location service parses the URL in the browser address bar (based on window.location) and makes the URL available to your application.
...
Getter and setter methods
// get the current path $location.path();
在你的控制器中使用
angular.module("app").controller("RecordsController", function($http, $routeParams, $location){
var path = $location.path();
// Do something with `path` ...
});
演示
导入 $locationProvider 并在控制器中检查 $location.path()
if($location.path() == '/record")