【问题标题】:AngularJS html5mode = true error when I access directly url with params当我使用参数直接访问 url 时,AngularJS html5mode = true 错误
【发布时间】:2013-11-05 15:59:43
【问题描述】:

我有一个使用 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 进行了修改以使其正常工作,但并不幸运。它回滚了。

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

【问题讨论】:

  • 我最近遇到了一个参数不工作的路由问题。当我将它从 /foo/:id 更改为 /foo/:id/bar 时它开始工作 - 不知道为什么:)
  • 你好@aweibell 上周当我将所有 index.html 内容更改为 Symfony View .twig 时,问题解决了,出现路由冲突并且 Htaccess 无法正常工作。现在它正在工作。谢谢

标签: javascript php .htaccess angularjs symfony


【解决方案1】:

您已将 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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2015-12-14
  • 2014-04-25
  • 1970-01-01
相关资源
最近更新 更多