我正在开发我的 Angular 应用程序并设置了这样的路线
angular.module('app', ['ngRoute']) .config(function ($routeProvider, $locationProvider) { $routeProvider.when('/', { template : "", controller : function($window) { $window.location.href = '/'; } }).when('/test', { redirectTo : '/test/dashboard' }).when('/test/dashboard', { templateUrl : '../partials/dashboard.html', controller : 'DashboardController' }).when('/test/unit', { templateUrl : '../partials/unit.html', controller : 'unitController' }); $locationProvider.html5Mode(true); });
HTML is
<body ng-app="app">
...
<div id="sidebar-wrapper" class="fill-height sidebar-nav">
<ul class="tabRow nav nav-stacked">
<li ng-class="{active: isActive('dashboard') }"><a id="dashboardLink" ng-href="#test/dashboard">Dashboard</a>
</li>
<li ng-class="{active: isActive('unit') }"><a id="unitLink" ng-href="#test/unit">Unit</a>
</li>
</ul>
</div>
...
</body>
问题:
当我输入“/test”但该页面的 html 未显示时,此代码会将 URL 重定向到“/test/dashboard”。但是,当我使用该路由刷新页面时,它会在页面的内容区域中显示 html。
当我在链接之间切换时,仪表板会显示出来,只是在第一次导航到时不会加载。
此外,当我将重定向更改为/test/unit 时,它会正确重定向并显示 unit.html。
不确定是什么导致了这种行为。
如有任何帮助,我们将不胜感激。
最佳答案
我认为您需要在为 $locationProvider
配置 html5Mode 时指定 requireBase: false
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
这是一个有效的 plnkr demo
有关与您的部署相关的更多上下文,您应该了解 this形成 Angular 文档
关于html - angularjs View 仅在硬刷新时加载而不是在重定向时加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30314698/