Angular JS - 转到其他状态而不更改URL

时间:2018-04-24 09:31:43

标签: angularjs

您好我已经使用stateprovider很长一段时间了,这次我想实现一个功能,在不更改网址的情况下,我想要not found state而不是网址。

这是我的代码。

(function () {
angular.module('mean').config(aosOfferConfig);

function aosOfferConfig($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/link/404_not_found");
    $stateProvider
        .state('offers', {
            url: '/offers/packages',
            templateUrl: 'views/aosOffers/aosoffers.html',
            controller: 'offersController',
            controllerAs: 'packages'
        })
        .state('offersignup', {
            url: '/offers/signup',
            templateUrl: 'views/aosOffers/offerssignup.html',
            controller: 'offersSignupController',
            controllerAs: 'offerssignup'
        })
        .state('thankyou', {
            url: '/offers/thankyou',
            templateUrl: 'views/aosOffers/offersthankyou.html'
        });
    }
})();

现在在我的另一部分,我将用户带到/link/404_not_found。我不想要这个。相反,我希望它将保留在当前URL中,但状态应该更改。就像github一样。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

这样做

(function () {
angular.module('mean').config(aosOfferConfig);

function aosOfferConfig($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise(function ($injector, $location) {
        var state = $injector.get('$state');
        state.go('404_not_found', {}, { location: false });
         return $location.path();
    });
    $stateProvider
        .state('offers', {
            url: '/offers/packages',
            templateUrl: 'views/aosOffers/aosoffers.html',
            controller: 'offersController',
            controllerAs: 'packages'
        })
        .state('offersignup', {
            url: '/offers/signup',
            templateUrl: 'views/aosOffers/offerssignup.html',
            controller: 'offersSignupController',
            controllerAs: 'offerssignup'
        })
        .state('thankyou', {
            url: '/offers/thankyou',
            templateUrl: 'views/aosOffers/offersthankyou.html'
        })
        .state('404_not_found', {
            template: '<h1>Notfound</h1>'
        });
  }
})();