Angular - 从子窗口更新父$ scope

时间:2014-12-30 16:27:33

标签: javascript angularjs angularjs-scope window.opener

以下是我要解决的问题。

我有一个window.open函数打开一个小窗口供用户使用他们选择的社交网络登录/注册。一旦用户登录,我希望小窗口关闭,并且原始父窗口的$scope无需刷新页面即可更新。

我的目标是不打断用户对网站的体验。该网站是一个音乐网站,因此如果用户正在收听歌曲并且页面重新加载,他们将失去他们在网站中的位置并最终不高兴。

我认为这就是全部。

app.controller('MainCtrl', [

  $scope.loginTrigger = function() {
    window.open("http://www.url.com/login", "_blank", "toolbar=yes, scrollbars=yes, resizable=no, top=0, left=100, width=1000, height=600");
  }

  if ($location.hash()) {
     $http({
      method: 'POST',
      url: '/users/oauth_complete/',
      data: data,
     }).success(function(data){
        if(!data.success){
         $window.location.href = '/usersetup';
        }else {
         $scope.LoggedInUser = data;
         storage.set('LoggedInUser', data);

         //Here is my current code: Small window closes and the parent page reloads but I don't want to use refresh as it will interrupt the user experience.
         window.close();
         window.opener.location.reload();
        }
     },"JSON");
  };

}]);

1 个答案:

答案 0 :(得分:0)

您可以使用$ emit和$ on

在类之间“交谈”

在登录控制器中,您将使用$ emit。

$scope.$emit('eventName', myData);

然后在你的另一个控制器中,你会使用$ on函数来监听:

$scope.$on('eventName', function(event, data) { console.log(data); });