如何修改hashbang url的角度

时间:2013-12-26 15:37:01

标签: javascript angularjs

我正在尝试添加#!到#而不是#。 (/#/ - > /#!/)。但是,当我尝试修改$ location时出现错误:(未知提供商:来自myApp的$ location)

app.js文件

'use strict';


// Declare app level module which depends on filters, and services
var myApp = angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'myApp.controllers']);

myApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'HomeController'});
        $routeProvider.when('/results', {templateUrl: 'partials/results.html', controller: 'ResultsController'});
        $routeProvider.when('/print/:param1', {templateUrl: 'partials/print.html', controller: 'PrintController'});
        $routeProvider.when('/results/:param1', {templateUrl: 'partials/results.html', controller: 'ResultsController'});

        $routeProvider.otherwise({redirectTo: '/home'});
  }]);

//enable hashbang (#!) url for google SEO
myApp.config(['$location', function($location) {
        $location.hashPrefix('!');
    }]);

1 个答案:

答案 0 :(得分:3)

您需要使用提供程序($locationProvider)来配置服务($location)..

myApp.config(['$locationProvider', function($locationProvider) {
     $locationProvider.hashPrefix('!');
}]);