【问题标题】:How to trap and get value from a redirected URL in angular.js?如何从 angular.js 中的重定向 URL 捕获并获取值?
【发布时间】:2016-05-06 06:24:46
【问题描述】:

我使用 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);

        };

}]);

【问题讨论】:

    标签: angularjs node.js


    【解决方案1】:

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

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

    然后在你的控制器中你可以调用:

    console.log($stateParams.code);
    

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

    【讨论】:

    • 当我调用旧 URL 时,它完全重定向到从服务器呈现的页面(登录页面)的新页面。
    • 我不确定您在说什么,但是您的代码和情况是什么,您可以按照我上面演示的方式捕获当前处于活动状态的 url 参数。
    • 你能用这东西做一个笨蛋吗?或者可能显示更多代码,例如您的 app.js?
    • 我接受我们可以捕获当前 URL 参数,但在我的场景中,当调用 URL (locahost:3000/auth/…) 时,它被重定向到新窗口(登录页面),此登录页面不是从我的本地它是从服务器呈现的。如需更多信息,请提供您的 Skype ID,我会 ping 您。
    • 嗨 Thepio in plunker 我添加了 app.js 代码 plnkr.co/edit/qaXkoKoycTku5crh9LXb?p=preview
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    相关资源
    最近更新 更多