简体   繁体   中英

Preserving back button in angular through a loading controller?

Depending on the logic in services, I route to different views. To facilitate service injection, I use a loading controller to determine where to go next.

This works great, except that I can't go "back" through a loading controller. How do I either detect the back button, or fix my loading model to be more friendly to it?

    .when('/loading', {
                templateUrl: '/app/views/mystuff/loading.html',
                controller: 'LoadingController'
            })

angular.module('app').controller('LoadingController', ['$location', 'WhereService',
    function ($location, WhereService) {
        if (!WhereService.doneWithA()) {
            $location.path('/a');
        }
        else if (!WhereService.doneWithB()) {
            $location.path('/b');
        }
        else {
            location.href = "/myapp/somewhereelseentirely"//this is fine
        }
    }]);

angular.module('app').controller('ALoadingController', ['$location', 'AService',
    function ($location, AService) {
        if (!AService.isDone()) {
            $location.path(AService.currentPath());
        }
        else {
            $location.path('/');//triggers loading controller above
        }
    }]);

Sounds like you need $location.replace() :

If called, all changes to $location during current $digest will be replacing current history record, instead of adding new one.

https://docs.angularjs.org/api/ng/service/ $location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM