AngularJS を使用して別のページにリダイレクトするにはどうすればいいですか? 質問する

AngularJS を使用して別のページにリダイレクトするにはどうすればいいですか? 質問する

サービス ファイルで機能を実行するために Ajax 呼び出しを使用しており、応答が成功した場合は、ページを別の URL にリダイレクトしたいと考えています。現在、私はこれをプレーンな JS コードで実行していますwindow.location = response['message'];。ただし、これを AngularJS コードに置き換える必要があります。stackoverflow でさまざまなソリューションを調べましたが、それらは を使用しました$location。しかし、私は AngularJS を初めて使用しており、実装に問題があります。

$http({
            url: RootURL+'app-code/common.service.php',
            method: "POST",
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            dataType: 'json',
            data:data + '&method=signin'

        }).success(function (response) {

            console.log(response);

            if (response['code'] == '420') {

                $scope.message = response['message'];
                $scope.loginPassword = '';
            }
            else if (response['code'] != '200'){

                $scope.message = response['message'];
                $scope.loginPassword = '';
            }
            else {
                window.location = response['message'];
            }
            //  $scope.users = data.users;    // assign  $scope.persons here as promise is resolved here
        })

ベストアンサー1

Angular を使用できます$window:

$window.location.href = '/index.html';

コントローラーでの使用例:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('LoginCtrl', LoginCtrl);

    LoginCtrl.$inject = ['$window', 'loginSrv', 'notify'];

    function LoginCtrl($window, loginSrv, notify) {
        /* jshint validthis:true */
        var vm = this;
        vm.validateUser = function () {
             loginSrv.validateLogin(vm.username, vm.password).then(function (data) {          
                if (data.isValidUser) {    
                    $window.location.href = '/index.html';
                }
                else
                    alert('Login incorrect');
            });
        }
    }
})();

おすすめ記事