怎么在angularjs的config中使用service

2025-01-03 15:19:47
推荐回答(1个)
回答1:

var app = app.module('myApp', ['ngAnimate', 'ui.router']);
app.constant('referrers', []);
app.config(function ($stateProvider, $urlRouterProvider, referrers) {
    $urlRouterProvider.otherwise('/');
    
    $stateProvider
        .state('/', {
            url: '/',
            templateUrl: 'views/list.html',
            onExit: function ($state) {
                referrers.push({name: $state.current.name, params: $state.params});
            }
        })
        .state('profile', {
            url: '/profile',
            templateUrl: 'views/list.html',
            onExit: function ($state) {
                referrers.push({name: $state.current.name, params: $state.params});
            }
        })
});

app.controller('myController', ['$scope', '$state', 'referrers', '$timeout', function ($scope, $state, referrers, $timeout) {
    $scope.isBack = false;
    $scope.viewBack = function () {

        if (referrers.length == 0) return false;
        $scope.isBack = true;
        // 删除记录并返回页面
        var prev = referrers.pop();
        $state.go(prev.name, prev.params);
    
        $timeout(function () {
            referrers.pop();
             $scope.isBack = false;
        }, 350);
    };
}])

onExit: function ($state, yourService) { }