【问题标题】:Angularjs stateProvider not working with xampp. It only works with nodejsAngularjs stateProvider 不适用于 xampp。它仅适用于 nodejs
【发布时间】:2018-10-06 23:54:20
【问题描述】:

我正在用 angularjs 和 PHP 开发一个博客。使用 nodejs 一切正常,我可以使用“stateProvider”来路由应用程序。现在我正在使用 xampp,但是当我在 url 中插入“localhost / home / content”类型的地址时告诉我“找不到对象”,就好像实际上要在服务器上查找对象而不是使用路由功能一样angularjs的。相反,如果我插入“localhost / index_2.html”,它实际上会正确显示页面,并且 url 变为“localhost / home / content”(正如我所期望的那样)。我该如何解决?谢谢!

app.js

var importPath = "/partial/";
  var home = importPath+"home/";

  var homeContainer = home + 'homeContainer.html';
  var homeContent = home + 'homeContent.html';

  app.config(
    ["$stateProvider", "$urlRouterProvider", "$locationProvider",
      function($stateProvider, $urlRouterProvider, $locationProvider) {

      $stateProvider
        .state('home', {
          templateUrl: homeContainer,
        })
        .state('home.homeContent', {
          url: '/home/content',
          templateUrl: homeContent,
        });


       $urlRouterProvider.otherwise("/home/content");
       $locationProvider.html5Mode(true);


      }
    ]);

  app.run(['$rootScope', '$location',
      function ($rootScope, $location)
      {
        console.log("location ", $location.path());

      }
  ]);

index_2.html

......
<body>
...
<div ui-view></div>
...
</body>

【问题讨论】:

  • 可能是您的html5Mode 在没有哈希爆炸的情况下中断了路由 (#!)
  • 这与 xampp 或 nodejs 无关。这是关于你使用$locationProvider.html5Mode(true)
  • 我不应该使用html5模式吗?那么html5模式的目的是什么?谢谢
  • 它删除了 hash-bang,将 URL 从 www.website.com/#!/some/other/path 转换为 www.website.com/some/other/path。看起来 专业,但它会破坏您的路由,因此您必须手动修复它(通常在 .htaccess 中)。如果你可以避免它,那么就不要使用它。删除它后,确保您的链接正常工作:href="/link" 应该是 href="#!/link" 等(ui.router 只需使用状态:ui-sref="state"
  • 谢谢大家,我删除了 html5 模式,现在它可以正常工作了。

标签: php angularjs node.js routing xampp


【解决方案1】:

根据收到的 cmets,我可以通过删除 index.html 文件中的 html5 模式和标签 &lt;base href="/"&gt; 来解决。 现在基本网址是http: // localhost / #! / Home / content

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2011-03-04
    • 2011-10-01
    • 1970-01-01
    • 2013-03-09
    • 2020-01-04
    相关资源
    最近更新 更多