2

これは簡単なことかもしれませんが、私は angularjs の初心者です。これが私のangularjsコントローラーコードです

    function MyCtrl1($scope, $location, $rootScope) {
  $scope.$on('$locationChangeStart', function (event, next, current) {
    event.preventDefault();
    var answer = confirm("Are you sure you want to leave this page?");
    if (answer) {

    }
  });
}
MyCtrl1.$inject = ['$scope', '$location', '$rootScope'];

変数には、確認時にリダイレクトするURLがありnextます.しかし、angularjsでこれを達成する方法.

4

3 に答える 3

7

手動で行う必要はありません。確認が得られない場合にのみ、イベントをキャンセルしてください。

$scope.$on('$locationChangeStart', function (event, next, current) {
    if ( ! confirm("Are you sure you want to leave this page?") ) {
        event.preventDefault();
    }
});
于 2013-02-12T20:22:39.093 に答える
6

他の回答に記載されているようにプレーンなjavascriptを使用するか、このようにangularが提供する$ locationサービスを使用できます

    $location.path(next)

また

   $location.replace(next)

最初のものは現在のパスに追加されます(ほとんどが部分的です)

2 つ目は現在のパスを置き換えます (google.com から yahoo.com など)。

于 2013-02-12T20:27:56.990 に答える
-4

window.location = 'myURL'

これがMDNドキュメントです

于 2013-02-12T20:22:39.710 に答える