对于这样定义的路由:
$routeProvider
.when('/',
{
templateUrl:'views/login.html',
controller:'Login',
private:false
});
例如,如何访问事件中的private
属性$routeChangeStart
?目前我正在使用current.$$route.private
它,但它似乎是错误的。
谢谢。
对于这样定义的路由:
$routeProvider
.when('/',
{
templateUrl:'views/login.html',
controller:'Login',
private:false
});
例如,如何访问事件中的private
属性$routeChangeStart
?目前我正在使用current.$$route.private
它,但它似乎是错误的。
谢谢。
实际上,建议将所有带有路由的自定义数据放在“数据”对象中。
$routeProvider
.when('/',
{
templateUrl:'views/login.html',
controller:'Login',
data: {
private: false
}
});
这是我访问路由参数的方式
$rootScope.$on( "$routeChangeStart", function(event, next, current) {
next.data.private;
});
routeChangeStart 事件的第二个参数是调用的路由对象。另一个优点是data
对象中的任何内容都可以传递给子状态。
$routeChangeStart
发生在路线更改之前,因此您需要查看next
. 没有必要使用next.$$route
,因为 next继承自 $$route
.
angular.module('example', ['ngRoute'])
.config(function($routeProvider) {
$routeProvider.when('/', {
controller: 'MyCtrl',
template: '<b>isPrivate: {{isPrivate}}</b>',
private: false
});
})
.run(function($rootScope) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
/*
* this is fired prior to the route changing, so your params will be on
* next. Here we just attach it $rootScope as an example.
* note that you don't need to use next.$$route since $$route is private,
* and next inherits from next.$$route. */
*/
$rootScope.isPrivate = next['private'];
});
})
.controller('MyCtrl', function($scope) {
})