javascript - 在 Angularjs 中使用 "peek through"处理 neSTLed ng-click

标签 javascript angularjs angularjs-directive

我在处理多个嵌套的 angularjs 鼠标按钮指令时遇到了一些麻烦。 我有一个如下所示的模板:

<div class="project-wrap">
    <div class="workspace-wrap">
        <div id="workspace" class="workspace"
             oncontextmenu="return false;" 
             ng-mousedown="project.checkBtnDown($event)" 
             ng-mouseup="project.checkBtnUp($event)" 
             ng-mousemove="project.mouseMoved($event)">
        </div>
    </div>

    <button class="logoutBtn" ng-click="project.logout()">
        Logout
    </button>
</div>

checkBtnDown() 的作用是简单地检查按下了哪个鼠标按钮,然后对其进行处理。

我遇到的问题是,当在“工作区”(在我的 ProjectCtrl 模板内)上按下鼠标左键时,它会在“工作区”div 内放置一个 SVG 元素。此 SVG 元素与自定义 Angular Directive(指令)绑定(bind),该指令在其模板上有一个 ng-click。

所以发生的事情是,我按计划创建了 SVG 元素,但是当我单击 SVG 元素的部分时,我想在范围内调用函数。它仍在调用 checkBtnDown(),因为新的 SVG 元素位于项目模板内。

如何让 SVG 元素 ng-click 来“浏览”而不同时触发 checkBtnDown()?

最佳答案

Hi, i don't know if you meaning this or not..hope this helps you.

in fact you just need to detect if your mouse clicked or not, for that we need to something to detect this for second time.

        var app = angular.module("app", []);

        app.controller("ctrl", function ($scope) {

            $scope.alreadyClicked = false;

            $scope.action = function() {
                console.log("action");
            }

            $scope.mousedown = function (event) {
                if ($scope.alreadyClicked) {
                    $scope.action();
                } else {
                    console.log("mousedown");
                    $scope.alreadyClicked = true;
                }
            }

        });
        .box {
            position: relative;
            background: #eee;
            border: solid 1px #ccc;
            float: left;
            width: 100px;
            height: 100px;
          cursor:pointer
        }
<!DOCTYPE html>
<html ng-app="app" ng-controller="ctrl">
<head>
    <title></title>
</head>
<body>

    <div class="box" ng-mousedown="mousedown($event)">
        {{alreadyClicked ? "click to call action":"click to call mousedown"}}
    </div>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


</body>
</html>

关于javascript - 在 Angularjs 中使用 "peek through"处理 neSTLed ng-click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38024875/

相关文章:

css - AngularJS 从元素上的不同指令累积样式

javascript - 当在模板内部使用粘性指令时,ngSticky 插件不起作用

javascript - 根据字符串数组过滤对象数组

javascript - 新消息追加时向下滚动底部

javascript - 使用 IE 在 Angular 模板中设置 HTML 样式时出现问题

angularjs - Angular 1.4.1 $cookies 最终成为 session cookie

javascript - 下拉列表未显示在 Bootstrap 输入组中

javascript - 如何使用 React Native 中的 React Native 元素库中的复选框从平面列表中选择多个项目?

angularjs - 如何通过 Node 服务器将 JSON 对象写入文件?

javascript - 在所有子元素中继承 angularjs 函数