首页 > 怎么在angularjs的config中使用service

怎么在angularjs的config中使用service

我最近想用angularjs写一个chrome app,但是chrome app禁用了history API。所以我想自己用angularjs的ui-router记录历史url来模拟页面返回,大致代码如下:

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);
    };
}])

现在我想把referrers.push({name: $state.current.name, params: $state.params});这段代码写到一个公共的方法里面,但是发现config无法注入service,该怎么做


onExit: function ($state, yourService) { }

Provider

【热门文章】
【热门文章】