简体   繁体   English

我可以观看angular js中的点击吗? 我需要关闭从外部事件调度的弹出窗口

[英]Can i watch clicks in angular js? I need to close a pop up dispatched from an external event

I have a huge application with a lot of information and different functionalities. 我有大量的信息和不同的功能的巨大应用程序。 There is an external d3 directive (the d3 graphic is embebed on the view that im working on) and i have another popup that show up when i click in some points on the graphic. 有一个外部d3指令(d3图形嵌入在我正在处理的视图中),当我单击图形上的某些点时,会显示另一个弹出窗口。

Some event is fired in that directive (is external and i dont have to do anything there) with a $scope.$emit("someEvent") , I received it in my controller with $scope.$on("someEvent", function(event){//here a pop up with information is opened}) 使用$scope.$emit("someEvent")在该指令中触发了某个事件(在外部,我不必在其中做任何事情$scope.$emit("someEvent") ,我在控制器中使用$scope.$on("someEvent", function(event){//here a pop up with information is opened})

My functionality needs that a few clicks still firing news pop ups (always the last one will be shown and that is already solved with the emit of someEvent) the problem is with the rest of the clicks outside the pop up, how can I get them for closing it?. 我的功能需要仍然单击一些消息才能触发新闻弹出窗口(总是显示最后一个,并且已经通过发出someEvent来解决)问题是弹出窗口之外的其余单击,如何获得它们关闭吗?

Right now i have this structure in my controller 现在我的控制器中有这个结构

$scope.closePopup = function () {
  $scope.showPopup = false;
}

$scope.$on("someEvent", function(event){
  //here a pop up with information is opened not always is true
  //if a dont have information is false, this is just representative
  $scope.showPopup = true;
  if ($scope.showPopup) {
    $scope.$emit("popupOpened");
  }
}) 

$scope.$on("popupOpened",function (event) {
  //and here i need to add a listener o something for catching clicks 
  //and asociate them to the $scope.closePopup () function.
  //also, i need to know if thoose clicks are firing someEvent to...
})

I have to catch clicks from all the view, any click should be catched. 我必须抓住所有视图的点击,任何点击都应该被抓住。 I have tried with document.getElementById("idview").onclick = function () { $scope.closePopup() }; 我已经尝试使用document.getElementById("idview").onclick = function () { $scope.closePopup() }; in $scope.$on("someEvent") but this is fired the first time and never show up because is opened and closed in the same click. $scope.$on("someEvent")但这是第一次触发,并且因为在同一单击中打开和关闭而从未显示。 Thats the reason why I have refactor it with a new $emit 那就是为什么我用新的$ emit重构它的原因

I appreciate some approach or clue for doing this! 我感谢这样做的一些方法或线索!

Thanks! 谢谢!

To open / close a popup, you can simply add/remove a class on the popup container, or on the body tag of your document app. 要打开/关闭弹出窗口, 您只需在弹出容器或文档应用程序的body标签上添加/删除类

Example using $scope.showPopin that you've mentionned : 您提到的$scope.showPopin示例:

<button ng-click="showPopup = !showPopup">Toggle the popup !</button>

...

<div class="popin-container" ng-class="showPopup ? 'visible' : ''">
  <div class="popin-background" ng-click="showPopup = false"></div>
  <div class="popin"></div>
</div>

Assuming .popin-background is an element (transparent or not) behind your popin, then if you click wherever out of the popin (so : in .popin-background element) you will close the popin (= remove the .visible class). 假设.popin-background.popin-background一个元素(透明或不透明),那么如果您在弹出框之外的任何位置单击(所以: .popin-background元素中的内容),则会关闭该弹出框(=删除.visible类)。

Then you can handle its display / animation with CSS . 然后, 您可以使用CSS处理其显示/动画

I did not use your approach (event emitters / listeners) as it doesn't seems necessary here, Angular already listens to its scopes variables for change , so you can play with in your view. 我没有使用您的方法(事件发射器/侦听器),因为这里似乎没有必要, Angular已经在监听其scope变量以进行更改 ,因此您可以在视图中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM