javascript - 当我使用参数直接访问 url 时,AngularJS html5mode = true 错误

标签 javascript php .htaccess angularjs symfony

我有一个使用 Angularjs 和 Symfony 2 开发的应用程序,我启用了 html5 模式来获取友好的 URL。

该应用程序与 Symfony 2.3 集成以响应服务。

app.js

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

      $provide.decorator('$sniffer', function($delegate) {
            $delegate.history = false;
            return $delegate;
        });

    $routeProvider.when('/home', {templateUrl: 'views/home.html', controller: 'HomeController'});
    $routeProvider.when('/quienes_somos', {templateUrl: 'views/quienes_somos.html', controller: 'QuienesSomosController'});
    $routeProvider.when('/ubicanos', {templateUrl: 'views/ubicanos.html', controller: 'UbicanosController'});
    $routeProvider.when('/ubicanos/:route', {templateUrl: 'views/ubicanos.html', controller: 'UbicanosController'});
    $routeProvider.when('/catalogo', {templateUrl: 'views/catalogo.html', controller: 'CatalogoController' });
    $routeProvider.when('/catalogo/:category', {templateUrl: 'views/catalogo.html', controller: 'CatalogoController' });
    $routeProvider.when('/catalogo/:category/:productid', {templateUrl: 'views/catalogo.html', controller: 'CatalogoController'});
    $routeProvider.when('/catalogo/results',{templateUrl: 'views/catalogo.html', controller: 'CatalogoController'});
    $routeProvider.when('/noticias', {templateUrl: 'views/noticias.html', controller: 'NoticiasController'});

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

        $locationProvider.html5Mode(true);
  }]);

当我尝试直接放置类别的路由和参数来访问时,就会出现问题。

http://plastimex.localhost/catalogo/tablilla-plastica

Angularjs 无法加载。但其他路线工作正常。

路线不工作

 $routeProvider.when('/catalogo/:category', {templateUrl: 'views/catalogo.html', controller: 'CatalogoController' });

 $routeProvider.when('/catalogo/:category/:productid', {templateUrl: 'views/catalogo.html', controller: 'CatalogoController'});

我也在index.html上设置了这个<base href="/index.html" />

这是.htaccess

DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /app.php/
    </IfModule>
</IfModule>

我对 htaccess 进行了修改以使其正常工作,但并不幸运。它回滚了。

有什么想法吗?请。 谢谢。

最佳答案

您已将 html5mode 设置为 true,但您提到的示例 URL 看起来像是在使用 hashbang。这只是一个错字吗?

http://plastimex.localhost/index.html#/catalogo/tablilla-plastica

应该是

http://plastimex.localhost/catalogo/tablilla-plastica

抱歉,我本想将此作为评论,但我没有要点。

相关文档链接:http://docs.angularjs.org/guide/dev_guide.services.$location#hashbang-and-html5-modes

关于javascript - 当我使用参数直接访问 url 时,AngularJS html5mode = true 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19793249/

相关文章:

apache - 阻止 .htaccess 中的 IP 地址有多安全?

apache - 如何使用 htaccess 加载子域 url 的特定页面

javascript - 如何使用 jquery 或 JavaScript 将元素属性与 Bootstrap Popover html 绑定(bind)?

php - 如何使用 jQuery.ajax 和 jScroll 与 PHP 制作无限滚动页面

html - 未声明语言 理想情况下

php - 在 Ubuntu 中使用 Apache 安装 MYSQL 和 PHP

页面加载时未调用 JavaScript 函数

javascript - 'require(' ./app/routes.js')(app);' 是什么意思在此 Node 服务器示例中是什么意思?

javascript - 将时间格式化为小时 :minutes on a Google Charts y-axis

javascript - 在 JQuery 中创建级联元素