在Div上拖动时禁用ng-Click

时间:2015-04-23 11:43:15

标签: javascript angularjs html5 draggable

我已经使用附加到可拖动指令的小部件启用了展开和折叠。这样可以正常工作。

ISSUE:

扩展和折叠已添加到ng-click事件中,因此当用户拖动窗口小部件并将其删除时,ng-click也会被触发。当用户拖动小部件时,我不希望ng-click被触发。

这是我的代码:

HTML:

<div plumb-item  ng-repeat="widget in dashboard.widgets" ng-style="{ 'left':widget.left, 'top':widget.top, 'width':widget.width  }" data-identifier="{{widget.id}}" resizeable>
    <p ng-if="widget.divider">{{widget.chartConfig.title.text}}</p>
    <md-card ng-if="!widget.expanded && !widget.divider" ng-click="widget.expanded = !widget.expanded" ng-mouseover="hover = true" ng-mouseout="hover = false">
        <md-item layout="row">                                
            <div class="collapsed-row" layout="row" layout-align="start center" flex> 
               <div class="md-toolbar-tools">
                   <h3>
                <span>{{widget.chartConfig.title.text}}</span>
               </h3>
               </div>                      
            </div>                                 
        </md-item>
    </md-card>
    <md-card ng-if="widget.expanded && !widget.divider" ng-click="widget.expanded = !widget.expanded">
        <div layout="column">                                
            <md-card-content>   
                 <highchart id="{{widget.id}}" config="widget.chartConfig"  ></highchart>                         
            </md-card-content>
        </div>
    </md-card>
</div>

JS:

Draggable.JS

routerApp.directive('draggable', function() {
  return {
        // A = attribute, E = Element, C = Class and M = HTML Comment
        restrict:'A',
        //The link function is responsible for registering DOM listeners as well as updating the DOM.
        link: function(scope, element, attrs) {
          console.log("Let draggable item snap back to previous position");
          element.draggable({
                // let it go back to its original position

                revert:true
              });
        }
      };
    });

2 个答案:

答案 0 :(得分:1)

我做了一个指令(alClick),它只是ng-click但是如果按下持续时间超过750毫秒,则取消任何点击。因此,拖动或滑动时不会触发任何点击或点击事件。

<div al-click="widget.expanded = !widget.expanded"></div>

答案 1 :(得分:0)

尝试在指令

中添加此内容
routerApp.directive('draggable', function() {
  return {
        // A = attribute, E = Element, C = Class and M = HTML Comment
        restrict:'A',
        //The link function is responsible for registering DOM listeners as well as updating the DOM.
        link: function(scope, element, attrs) {

          console.log("Let draggable item snap back to previous position");
          element.draggable({

                 stop:function(event,ui) {
                          console.log("Check if its printing")
                          event.stopPropagation();
                                },

                            // let it go back to its original position

                revert:true
              });
            }
         };
     });

这会停止事件的进一步传播,因此可能会停止不需要的点击