角:从$ routeProvider PARAMS传递给控制器 [英] Angular: Passing params from $routeProvider to controller
本文介绍了角:从$ routeProvider PARAMS传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有调用相同的控制器多条路线,我想不同的变量传递给它。
I have multiple routes that invoke the same Controller and I would like to pass different variables to it.
// Example
$routeProvider.
when('/a', {
templateUrl: 'test.html',
controller: 'MyController' // should get passed 'exampleA'
}).
when('/b', {
templateUrl: 'test.html',
controller: 'MyController' // should get passed 'exampleB'
});
我知道我可以使用解决的对象:
I know that I could use the "resolve" object:
$routeProvider.
when('/a', {
templateUrl: 'test.html',
controller: 'MyController',
resolve: {test: function() { return true; }}
});
要传递一个值作为依赖性:
To pass a value as a dependency:
app.controller('MyController', ['$scope', 'test', function ($scope, test) {
console.log(test); // true
}
我的这种做法的问题是,我的应用程序崩溃,如果决心对象丢失其他路线,我想传递可选PARAMS。
My problem with that approach is that my app crashes if the resolve object is missing on other routes and I would like to pass optional params.
有没有办法具体PARAMS传递给控制器(从路由提供商)?
Is there any way to pass specific params to the Controller (from the route provider)?
感谢您
推荐答案
路由:
$routeProvider.
when('/a', {
templateUrl: 'test.html',
controller: 'MyController',
paramExample: 'exampleA'
}).
when('/b', {
templateUrl: 'test.html',
controller: 'MyController',
paramExample: 'exampleB'
});
交通:在您的控制器注入$路线然后用这个
Access: inject $route in your controller then use this
app.controller('MyController', ['$scope', '$route', function ($scope, $route) {
var paramValue = $route.current.$$route.paramExample;
console.log(paramValue);
}
这篇关于角:从$ routeProvider PARAMS传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文