代码之家  ›  专栏  ›  技术社区  ›  Ed Swangren

停止通过库动态创建的DOM元素的事件传播

  •  1
  • Ed Swangren  · 技术社区  · 7 年前

    我使用的是AngularJS 1.7.5、X-Editable和SmartTable。在表格行中,我有一个锚定,它通过x-editable打开一个select。我还在智能表中启用行选择。

    问题是,我不确定如何从支持和选择或取消选择表行的“选择”中停止单击事件。我编写了一个指令来禁止从打开select的a元素中单击,但是select是由x-editable库创建的(例如,不在我的html中)。

    https://plnkr.co/edit/kAKbgLg05uBA9etICxV7?p=preview

    <!DOCTYPE html>
    <html ng-app="myApp">
    
    <head>
      <script data-require="jquery@*" data-semver="3.2.1" src="https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js"></script>
      <script data-require="angular.js@1.7.0" data-semver="1.7.0" src="https://code.angularjs.org/1.7.0/angular.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-smart-table/2.1.8/smart-table.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/1.1.2/ui-bootstrap-tpls.js"></script>
      <script data-require="xeditable@*" data-semver="0.1.8" src="https://vitalets.github.io/angular-xeditable/dist/js/xeditable.js"></script>
      <link data-require="bootstrap-css@3.3.7" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
      <link rel="stylesheet" href="style.css" />
      <script src="script.js"></script>
    </head>
    
    <body ng-controller="myController as $ctrl">
      <table st-table="collection" class="table">
        <thead>
          <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Secret Identity</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="row in collection" st-select-row="row" st-select-mode="multiple">
            <td>{{ row.id }}</td>
            <td>{{ row.name }}</td>
            <td>
              <a href="#" editable-select="row.secretIdentity" data-value="{{ row.secretIdentity }}" class="editable editable-click" buttons="no" mode="inline" e-ng-options="i for i in options" stop-event="click">
                    {{ row.secretIdentity }}
                   </a>
            </td>
          </tr>
        </tbody>
      </table>
    </body>
    
    </html>
    
    angular
      .module('myApp', ['xeditable', 'smart-table', 'ui.bootstrap'])
      .controller('myController', ['$scope', 'editableOptions', function($scope, editableOptions) {
        editableOptions.theme = 'bs3';
    
        $scope.collection = [{
          name: 'Ed',
          id: 1,
          secretIdentity: 'Just some guy'
        }, {
          name: 'Tony',
          id: 2,
          secretIdentity: 'Iron Man'
        }, {
          name: 'Steve',
          id: 3,
          secretIdentity: 'Captain America'
        }, {
          name: 'Bruce',
          id: 4,
          secretIdentity: 'Hulk'
        }, {
          name: 'Clint',
          id: 5,
          secretIdentity: 'Hawkeye'
        }, {
          name: 'Natasha',
          id: 6,
          secretIdentity: 'Black Widow'
        }, ];
    
        $scope.options = ['Iron Man', 'Captain America', 'Hulk', 'Black Widow', 'Hawkeye', 'Just some guy'];
      }])
    
      .directive('stopEvent', () => {
        return {
          restrict: 'A',
          link: (scope, element, attr) => {
            if (attr && attr.stopEvent) {
              element.bind(attr.stopEvent, e => {
                e.stopPropagation();
              });
            }
          }
        };
      });
    

    是否有一种标准的方法来处理由x-editable创建的元素,它也可以很好地处理AngularJS?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Dhruv Prakash    7 年前

    你可以把你的 <a> 在一个 <div> 添加你的 stop-event 该部门的指令。

    <td>
      <div stop-event="click">
         <a href="#" 
             editable-select="row.secretIdentity" 
             data-value="{{ row.secretIdentity }}" 
             class="editable editable-click" 
             buttons="no" 
             mode="inline"
             e-ng-options="i for i in options"
          >
             {{ row.secretIdentity }}
          </a>
      </div>
    </td>