ngRouteは、存在しないページを指すように履歴を設定するため、新しいコントローラでwindow.location.hrefを使用することはお勧めしません(ユーザーがクリックすると、 404ページにリダイレクトされます)。私は試して失敗しました。私はSOここで問題別に自分の関連ソリューションにあなたを指すように希望
: https://stackoverflow.com/a/27938693/1863794
私はそれを採用し、あなたのシナリオに適用されます。私はng-viewのネストされたレイヤーを持たない限り、それが一度だけ宣言されるので、ng-viewの外にMainCtrl
を使用することはそれほど難しいとは思わない...私はコードの重複も見ない。私の場合は
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when(..)
.otherwise({redirectTo: 'http://yourExternalSite.com/404.html'});
}])
.controller('MainCtrl',[ // <- Use this controller outside of the ng-view!
'$rootScope','$window',
function($rootScope,$window){
$rootScope.$on("$routeChangeStart", function (event, next, current) {
// next.$$route <-not set when routed through 'otherwise' since none $route were matched
if (next && !next.$$route) {
event.preventDefault(); // Stops the ngRoute to proceed with all the history state logic
// We have to do it async so that the route callback
// can be cleanly completed first, so $timeout works too
$rootScope.$evalAsync(function() {
// next.redirectTo would equal be 'http://yourExternalSite.com/404.html'
$window.location.href = next.redirectTo;
});
}
});
}
]);
乾杯
は、この修正は、私が探していたまさに –