fix*_*int 5 javascript angularjs
我正在将大型服务器呈现的应用程序转换为使用Angular。由于大小,我们一次只做一次。在此转换期间,部分应用程序将使用Angular,而部分则不会。这意味着路由有时会在Angular应用中路由,有时它需要从旧世界过渡到新世界(轻松)或从新世界过渡到旧世界(更困难)。
理想情况下,我想将Angular应用程序中的某些页面转换(新世界)具体路由到适当的控制器,但是其他任何页面转换都应仅获取新的HTML页面(旧世界)。
我似乎不知道该怎么做。我认为我需要使用routeProvider和when/ otherwise,但是我发现没有太多有用的文档。
正如triggerNZ所说,您始终可以让控制器将未处理的路由重定向到外部。下面是 HTML 和 Javascript,展示了如何做到这一点。
超文本标记语言
<div ng-app="myApp">
<script type="text/ng-template" id="this.html">
This Page.
</script>
<script type="text/ng-template" id="that.html">
That Page.
</script>
<div>
<ul>
<li><a href="/this">This</a></li>
<li><a href="/that">That</a></li>
<li><a href="/other">Other</a></li>
</ul>
<ng-view></ng-view>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript
var app = angular.module("myApp", ['ngRoute']);
app.config(function ($routeProvider, $locationProvider) {
$routeProvider.when('/this', {
templateUrl: 'this.html'
}).when('/that', {
templateUrl: 'that.html'
}).otherwise({
template: "<div></div>",
controller: function ($window, $location, $rootScope) {
if (!$rootScope.isInitialLoad) {
$window.location.href = $location.absUrl();
}
}
});
$locationProvider.html5Mode(true);
});
app.run(function ($rootScope) {
$rootScope.$on('$routeChangeSuccess', function() {
$rootScope.isInitialLoad = (typeof $rootScope.isInitialLoad === "undefined");
});
});
Run Code Online (Sandbox Code Playgroud)