AngularJS:触发从子窗口加载视图(OAuth)

AngularJS: triggering view loading from child window (OAuth)

本文关键字:视图 加载 OAuth 窗口 AngularJS      更新时间:2023-09-26

我几乎确信这是实现我的目标的最佳实践(在AngularJS中管理OAuth),但这里是我所做的和不起作用的:

通过使用Hello.js(通过Angular全局注入器在Angular中注入),我在controllers.js文件中使用以下代码打开了OAuth弹出窗口(在我的情况下是Google+)。

  hello.init({
        google: MY_APPLICATION_ID
    },{redirect_uri:'app/_partials/'});
     $scope.doLogin = function(network) {
         console.log('Calling hello ' + network);
         hello.login(network,function(r){ // callback
            console.log("login successful..");
         });
     };

带有登录请求的弹出窗口打开(请注意,我不得不使用一个"假的"index.html视图,因为$routerProvider似乎无法在子窗口上正确处理URI重定向(找不到页面,但URI/auth在父窗口上会正常运行……因为所有其他路由器都能在它上完美工作。所以我不能只猜测问题出在子窗口。)。通过这种方式加载index.html,因为重定向是在AngularJS不存在的情况下完成的。

这是我的路线配置。

    $routeProvider.
        when("/userLogin", {templateUrl: "_partials/userLogin.html", controller: "webClientController"})
        .when("/dialInterface", {templateUrl: "_partials/dialInterface.html", controller: "webClientController"})
        .when("/error404", {templateUrl: "_partials/error404.html", controller: "webClientController"})
        .otherwise({redirectTo: '/error404'});

这是我弹出窗口的内容(假的"index.html")

<!-- temp workaround to manage oAuth since routeProvider doesn't match the REDIRECT URI correctly -->
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
    $("#go-to-dial-interface",opener.document).trigger('click');
    window.close();
</script>

这是视图的代码(由AngularJS正确加载)/userLogin

<md-content class="md-padding md-primary" style="height: 600px;padding: 24px;" layout-fill>
    <a ng-click="doLogin('google')" class="zocial googleplus">Sign in with Google+</a>
</md-content>

<a id="go-to-dial-interface" href="#dialInterface" style="visibility:hidden;"></a>

基本上逻辑(以及问题)是:

  1. 用户点击a标签,执行doLogin('google')(这会调用控制器中的函数,该函数包含对Hello.js的调用)
  2. 选择了一个谷歌帐户,然后重定向(在这种情况下,重定向到"假的"index.html,因为我不使用Angular路由来管理它
  3. 该index.html在OAuth弹出窗口中正确加载,javascript代码正确执行:触发器转到拨号接口的click()事件并关闭OAuth弹窗
  4. 触发的click()事件本应在主窗口中重定向到一个新视图(#dialInterface),但它没有:当前视图(userLogin)被重新加载(或者可能没有..),而新视图没有(当然浏览器地址栏中的URL没有变化)

任何更好的解决方案/改进也被广泛接受。

提前感谢

您是否也为/dialInterface视图准备好了webClientController?有控制台错误吗?