简体   繁体   English

$ routeProvider / $ locationProvider不是AngularJS Routing中的函数

[英]$routeProvider / $locationProvider not a function in AngularJS Routing

I'm trying to create so-called 'SEO-friendly' URLs in AngularJS. 我正在尝试在AngularJS中创建所谓的“SEO友好”URL。

In my script.js for the routing I have: 在我的script.js中我有路由:

app.config(['$routeProvider',
function($routeProvider) {
    $routeProvider.html5Mode(true);
    when('/blog', {
        templateUrl: 'blog.html',
        controller: 'BlogController'
    }).
    when('/page/ideas', {
        templateUrl: 'ideas.html',
        controller: 'IdeasController'
    }).
    otherwise({
        templateUrl: 'home.html'
    });
}]);

app.controller("BlogController", function($scope) {
    $scope.title = 'Blog';
});

app.controller("IdeasController", function($scope) {
    $scope.title = 'Ideas';
});

To remove the # from the URL, I am enabling the html5 mode with: 要从URL中删除#,我将启用html5模式:

$routeProvider.html5Mode(true);

however, this results in the following error: 但是,这会导致以下错误:

Failed to instantiate module exampleApp due to: TypeError: $routeProvider.html5Mode is not a function 由于以下原因无法实例化模块exampleApp:TypeError:$ routeProvider.html5Mode不是函数

Does anyone have a solution for this issue? 有没有人有这个问题的解决方案? It means that the content will not display from the views because of it. 这意味着内容不会因为它而从视图中显示。

Edit : for anyone wondering, the working code is: 编辑 :对于任何想知道的人,工作代码是:

app.config(['$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider.
        when('/blog', {
            templateUrl: 'blog.html',
            controller: 'BlogController'
        }).
        when('/page/ideas', {
            templateUrl: 'ideas.html',
            controller: 'IdeasController'
        }).
        otherwise({
            templateUrl: 'home.html'
        });
        $locationProvider.html5Mode(true);
    }]);

html5mode method is available there on $locationProvider provider. $locationProvider提供程序上有html5mode方法。

You should include $locationProvider in your config phase to make that dependency available in config block & then enable html5mode for # free URL. 你应该包括$locationProvider在你的配置阶段,使这种依赖提供配置块&然后启用html5mode#免费URL。

Code

app.config(['$routeProvider', '$locationProvider',
function($routeProvider, $locationProvider) {

   //$routerProvider code here

   $locationProvider.html5Mode(true);

}]);

Additionally you have to add <base href="/" > tag on the index.html page 此外,您必须在index.html页面上添加<base href="/" >标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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