【问题标题】:Issue with direct URLS of Angular1.3 routing - even when html5mode is falseAngular 1.3 路由的直接 URL 问题 - 即使 html5mode 为 false
【发布时间】:2015-02-15 10:52:51
【问题描述】:

如果我正在浏览链接,我可以打开相应的视图,但如果我复制粘贴整个 url,视图不会加载并且控制台中没有错误。

这是我的主应用模块配置,

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

        $routeProvider
                .when('/welcome', {
                    templateUrl: 'src/app/welcome/welcome.html'
                })
                .when('/terminology', {
                    controller: 'TermsController',
                    controllerAs: 'vm',
                    templateUrl: 'src/app/terminology/term-table.html'
                })
                .when('/', {
                    redirectTo: '/welcome'
                })
                .otherwise({
                    redirectTo: '/welcome'
                });

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

例如: 1. 如果我点击主页链接,欢迎视图正在加载 ng-view 并且 URL 变为 (URL1) "http://localhost:8081/web/#/welcome"

  1. 如果我单击术语链接,术语视图将加载到 ng-view 中,并且 URL 变为 (URL2) "http://localhost:8081/web/#/terminology"

但如果我将整个 URL 复制粘贴到新选项卡中,则视图变为空白。控制台中没有错误。我正在使用 Angular1.3

添加: 此外,第一次默认视图也没有加载...... 提前致谢

【问题讨论】:

  • 你的意思是你得到 404 错误?
  • 没有……没什么……只是空白页。 ng-view 中没有加载模板
  • 即使是第一次,默认视图也没有加载.. :( 不知道我哪里错了
  • 查看文章bennadel.com/blog/…
  • @Nikivancici,我已经实现了很多次。但在 Angular1.3 中我面临这个问题 :(

标签: javascript angularjs routes


【解决方案1】:

我找到了解决方案。 我实际的 index.html 文件是这样的

<html class="no-js" lang="en" ng-app="app">
<head>  
    <!-- my css files -->
</head>
<body>
    <!-- this shell page contains "ng-view" directive which is causing the whole problem -->
    <div ng-include="'src/app/igl-layout/shell.html'"></div>

    <!-- my js files -->
</body>

现在我将 shell.html 的内容替换为上面的 ng-include 指令(行),然后一切正常。

谁能告诉我为什么会这样? 如果 ng-view 在 shell 页面内,它不会按预期工作,但如果它在 index.html 页面中,它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 2017-02-10
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2016-03-09
    • 2015-08-22
    相关资源
    最近更新 更多