否则在保留先前的URI时redirectTo'/ not-found'

时间:2014-09-19 09:51:20

标签: angularjs angularjs-routing

我在路由器上添加了404页面。所以这是我的代码:

app.js

$routeProvider.otherwise({
    redirectTo: '/not-found'
});

error.js

app.error = angular.module('CRM.Error', ['ngRoute', 'oc.lazyLoad']);

app.error.config(['$routeProvider', function ($routeProvider) {

    var routes = {
        '/not-found': {
            templateUrl: 'app/modules/error/views/not_found.html',
            controller: 'NotFoundController',
            page: {
                title: '404 Not found',
                name: 'notfound',
                require_auth: false
            },
            resolve: {
                lazy: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'CRM.Error',
                        files: [
                            'app/modules/error/controllers/not_found.controller.js'
                        ]
                    });
                }]
            }
        }
    };

    for (var path in routes) {
        $routeProvider.when(path, routes[path]);
    }

}]);

not_found.controller.js

app.error.controller('NotFoundController', ['$scope', '$location', function ($scope, $location) {
    console.log($location);
}]);

效果很好,我的视图已加载。

现在我想打印一条消息,例如"抱歉,但您尝试查看的网页[网址]不存在。" 但是我在/not-found URI上,所以我需要检索以前的URI。

我尝试了 app.js

中的内容
$rootScope.$on('$routeChangeStart', function(event, next, current) {

$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {

current向我提供/not-found个信息,previousnextundefined

请问好吗?

2 个答案:

答案 0 :(得分:0)

这可能会有所帮助:

// firing an event upwards
$scope.$emit('myCustomEvent', 'Data to send');

// firing an event downwards
$scope.$broadcast('myCustomEvent', {
  someProp: 'Sending you an Object!' // send whatever you want
});

// listen for the event in the relevant $scope
$scope.$on('myCustomEvent', function (event, data) {
  console.log(data); // 'Data to send'
});

答案 1 :(得分:0)

您使用UI路由器吗?您必须对路由器的行为进行一些修复。以前的状态正常。

替换当前,正常,标准的路线方法。