带参数的htm5mode对我不起作用

时间:2014-10-04 16:20:49

标签: angularjs angularjs-routing location-provider

第一手资料,这是我的代码:

配置应用:

promoApp.config(['$routeProvider','$locationProvider',function($routeProvider,$locationProvider){
        $locationProvider.html5Mode(true);
        $routeProvider.
        when('/home',{
            templateUrl: 'views/home.html',
            controller: 'HomeController'
        }).
        when('/web',{
            templateUrl: 'views/web.html',
            controller: 'WebController'
        }).
        when('/portfolio',{
            templateUrl: 'views/portfolio.html'
        }).
        when('/game',{
            templateUrl: 'views/game.html'
        }).
        when('/contact',{
            templateUrl: 'views/contact.html',
            controller: 'ContactController'
        }).
        when('/admin',{
            templateUrl: 'views/admin.html',
            controller: 'AdminController'
        }).
        when('/blog_admin',{
            templateUrl: 'views/blog_admin.html',
            controller: 'BlogAdminController'
        }).
        when('/blog_edit', {
            templateUrl: 'views/blog_edit.html',
            controller: 'BlogEditController'
        }).
        when('/blog_edit/:blogId', {
            templateUrl: 'views/blog_edit_post.html',
            controller: 'BlogEditPostController'
        }).
        when('/blog',{
            templateUrl: 'views/blog.html',
            controller: 'BlogController'
        }).
        when('/blog/:blogId',{
            templateUrl: 'views/blog_post.html',
            controller: 'BlogPostController'
        }).
        otherwise({
            redirectTo: '/home'
        });
    }]);

我的控制器BlogPostController:

blog.controller('BlogPostController',['$scope','$http','$upload','$routeParams','$sce',function($scope,$http,$upload,$routeParams,$sce){
        $scope.blogId = $routeParams.blogId;
        $scope.$upload = $upload.upload({
            method: 'POST',
            url: 'edit.php',
            data: {'title': $scope.blogId}
        }).success(function(data){
            if(data.error){
                $scope.resultMessage = data.message;
                $scope.result = 'bg-danger';
            }else{
                $scope.post = data;
                $scope.pastToPlainText = $sce.trustAsHtml;
            }
        });

        $http.get('backend/muestra.php').success(function(data){
            if(data.error){
                $scope.resultMessage = data.message;
                $scope.result = 'bg-danger';
            }else{
                $scope.entradas = data;
            }
        }).error(function(){
            $scope.resultMessage = "Se ha producido un error al consultar con la base de datos";
        });
    }]);

我的blog.html

<div class="container">
    <div class="row">
        <div class="col-lg-3 position-panel-blog hidden-md hidden-sm hidden-xs">
            <div class="panel panel-primary margin-index">
                <div class="panel-heading">
                    <h3 class="panel-title">Entradas recientes</h3>
                </div>
                <div class="panel-body">
                    <div ng-repeat="entrada in entradas | limitTo: 5">
                        {{$index + 1}}. <a ng-href="/blog/{{entrada.title}}" class="link-blog">{{entrada.title}}</a>
                    </div>
                </div>
            </div>
            <div class="panel panel-primary">
                <div class="panel-heading">
                    <h3 class="panel-title">Nuestros servicios web</h3>
                </div>
                <div class="panel-body">
                    <img src="images/que-ofrecemos/portada_web.jpg" alt="Nuestros servicios web" class="image-anuncio">
                    <div class="acceder">
                        <a href="#/web" class="btn btn-primary">Accede ahora</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="blog-position col-lg-8" ng-repeat="entrada in entradas">
            <div class="thumbnail">
                <div class="margin-content-blog">
                    <a ng-href="/blog/{{entrada.title}}" class="link-blog"><h1 class="format-title-blog">{{entrada.title}}</h1></a>
                    <div ng-bind-html="pastToPlainText(entrada.text)" class="text-blog"></div>
                    <slick infinite="false" dots="true" slides-to-show="3" slides-to-scroll="1">
                        <div ng-repeat="image in entrada.images track by $index">
                            <img src="{{image}}" alt="Imagen de la entrada {{entrada.title}}" class="image-post img-responsive">
                        </div>
                    </slick>
                    <div class="row">
                        <div class="col-lg-4">
                            <!-- Inserta esta etiqueta en la sección "head" o justo antes de la etiqueta "body" de cierre. -->
                            <script src="https://apis.google.com/js/platform.js" async defer>
                                {lang: 'es'}
                            </script>
                            <!-- Inserta esta etiqueta donde quieras que aparezca Botón Compartir. -->
                            <div class="g-plus" data-action="share"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

单击转到帖子时,模板blog_post.html根据$ route.current.templateUrl加载,但浏览器显示的内容与blog.html相同,但不显示blog_post.html模板的内容。没有html5mode,这可以正常工作,没有问题。也许html5mode与参数链接(/ blog_edit /:blogId)不兼容。下面我用图片更好地解释一下。

使用html5mode启用:

  1. 我在博客菜单中,我要点击帖子:(模板是blog.html) enter image description here

  2. 点击发布后,当前路由器会将模板更改为blog_post.html,但浏览器会显示相同的模板blog.html: enter image description here

  3. 使用html5mode禁用:

    1. 当我在blog.html模板中时,我要点击帖子: enter image description here

    2. 当我点击帖子时。现在它正确显示了blog_post.html模板: enter image description here

1 个答案:

答案 0 :(得分:2)

要支持在HTML5模式下创建的URL的直接链接,您需要在服务器上重写URL。例如。如果你使用Apache,请通过mod_rewrite

来自the documentation

  

服务器端

     

使用此[HTML5]模式需要在服务器端重写URL,基本上您必须重写所有指向应用程序入口点的链接(例如index.html)

如果您已经这样做,请同时提供相关的服务器配置。