如何重定向到采用了棱角分明JS另一个页面? [英] How to redirect to another page using Angular JS?

查看:171
本文介绍了如何重定向到采用了棱角分明JS另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ajax调用在服务文件执行的功能,如果反应是成功的,我想在页面重定向到另一个网址。目前,我用简单的JS这样做了window.location =响应['消息'];。但我需要angularjs code来代替它。我已经在计算器看着各种解决方案,他们用$位置。但是,我是新来的角度和有麻烦来实现它。

  $ HTTP({
            网址:将把rootURL +'APP-code / common.service.php',
            方法:POST,
            标题:{内容类型:应用程序/ x-WWW的形式urlen codeD},
            数据类型:JSON,
            数据:数据+'和;方法=登入        })。成功(功能(响应){            的console.log(响应);            如果(响应['code'] =='420'){                $ scope.message =响应['消息'];
                $ scope.loginPassword ='​​';
            }
            否则,如果(响应['code']!='200'){                $ scope.message =响应['消息'];
                $ scope.loginPassword ='​​';
            }
            其他{
                window.location的响应= ['消息'];
            }
            // $ scope.users = data.users; //此处分配$ scope.persons作为承诺这里解决
        })


解决方案

您可以使用角 $窗口

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

用法示例中位指示:

 (函数(){
    使用严格的;    角
        .module(应用)
        .controller('LoginCtrl',LoginCtrl);    。LoginCtrl $注='$窗口','loginSrv','通知'];    功能LoginCtrl($窗口,loginSrv通知){
        / * jshint validthis:真* /
        VAR VM =这一点;
        vm.validateUser =功能(){
             loginSrv.validateLogin(vm.username,vm.password)。然后(功能(数据){
                如果(data.isValidUser){
                    $ window.location.href ='/index.html';
                }
                其他
                    警报('登录不正确);
            });
        }
    }
})();

I am using ajax call to perform functionality in a service file and if the response is successful, I want to redirect the page to another url. Currently, I am doing this by using simple js "window.location = response['message'];". But I need to replace it with angularjs code. I have looked various solutions on stackoverflow, they used $location. But I am new to angular and having trouble to implement it.

$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
        })

解决方案

You can use Angular $window:

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

Example usage in a contoller:

(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');
            });
        }
    }
})();

这篇关于如何重定向到采用了棱角分明JS另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆