angularjs – 如何从angular.js中的重定向URL捕获并获取值?

我使用angular.js作为前端,node.js作为后端,现在我调用一个URL,下面在我本地主页的按钮点击事件中提到.

示例网址:

https://locahost:3000/auth/authorize?response_type=code&client_id=abzefcbbkjajsdd&scope=userfetchit&redirect_uri=localhost&nonce=32324

当我调用上面的URL时,它会在新窗口中显示登录页面,在我给出电子邮件和密码后,它会被重定向到另一个URL,如下所述

重定向的网址:

https://localhost:3000/auth/localhost?code=8dacae52ee284db3dc776aa6d3563912

预期结果 :

现在,我想从angular.js中的上述URL获取代码值“8dacae52ee284db3dc776aa6d3563912”.

HTML代码:

<html ng-app='app' ng-controller='LoginController'>

<head>
</head>

<body>
    <div class="container">

        <div class="jumbotron">
            <button type="button"

                class="btn btn-primary btn-lg"  ng-click="tokenLogin()">
                     New Login 
                </button>
        </div>
    </div>
    <pre>{{ cleanData | json}}</pre>
</body>

</html>

控制器代码:

angular.module('app',[])

.controller('LoginController',['$location','$window','$scope','$filter',function($location,$window,$scope,$filter) {
$scope.tokenLogin = function() {

            var url = 'https://localhost:3000/auth/authorize?response_type=code&client_id=abzefcbbkjajsdd&scope=userfetchit&redirect_uri=localhost&nonce=32324';
            $window.open(url);

        };

}]);

解决方法

首先,您需要在$stateProvider中添加代码参数,如下所示:

.state('yourstate',{
  url: '/localhost?:code',templateUrl: 'localhost.html'
});

然后在你的控制器中你可以打电话:

console.log($stateParams.code);

就是这样.您可以将此值设置为范围变量或任何内容.

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...