angularjs – 如何从angular.js中的重定向URL捕获并获取值?
发布时间:2020-12-17 16:55:32 所属栏目:安全 来源:网络整理
导读:我使用angular.js作为前端,node.js作为后端,现在我调用一个URL,下面在我本地主页的按钮点击事件中提到. 示例网址: https://locahost:3000/auth/authorize?response_type=codeclient_id=abzefcbbkjajsddscope=userfetchitredirect_uri=localhostnonce=32324
我使用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); 就是这样.您可以将此值设置为范围变量或任何内容. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |