【问题标题】:$event.stopPropagation doesn't play nice with Bootstrap data-toggle$event.stopPropagation 不适合 Bootstrap 数据切换
【发布时间】:2017-05-04 08:49:46
【问题描述】:

我有一个元素列表,每个元素都包含一个 ng-click。嵌套在每个元素内的是一个 div,它应该切换 Bootstrap 模式。

我已将 $event.stopPropagation 添加到嵌套 div 中,因为我不希望父元素中的 ng-click 事件触发,但这会导致模式无法开始显示。

    <div id="segment{{segment.Id}}" class="message" ng-class="{activeSegment: segment.Selected}" ng-click="!segmentIsLoaded || selectSegment(segment)">
        <div>
             <div class="pull-left">
                 <span>ID: {{segment.Id}}</span>
                 <span>{{segment.Name}}  </span>
                 <i class="fa fa-info-circle fa-lg" data-toggle="tooltip" data-original-title="{{segment.Description}}" tooltip></i><br />
                                        <small>{{formatJsonDate(segment.Updated)}}</small>
              </div>
              <div class="btn btn-danger pull-right" data-toggle="modal" data-target="#deleteSegmentModal" ng-click="$event.stopPropagation();">
                   <span><i class="fa fa-trash-o" aria-hidden="true"></i></span>
              </div>
         </div>
     </div>

有任何已知的解决方法吗?

我也许应该提到 stopPropagation() 的工作原理是防止点击事件触发。我的问题是引导模式没有通过 data-toggle 属性激活

【问题讨论】:

  • “有任何已知的解决方法吗?” 以编程方式打开模式。 每个元素内都嵌套了一个按钮"不,没有。
  • 每个元素内嵌套一个按钮将其改为每个元素内嵌套一个div快乐吗?跨度>

标签: angularjs twitter-bootstrap


【解决方案1】:

您是否尝试过使用指令

<a ng-click='expression' stop-event='click'>
.directive('stopEvent', function () {
    return {
        restrict: 'A',
        link: function (scope, element, attr) {
            if(attr && attr.stopEvent)
                element.bind(attr.stopEvent, function (e) {
                    e.stopPropagation();
                });
        }
    };
 });

查看SO链接How can I make an AngularJS directive to stopPropagation?

【讨论】:

  • 您能解释一下为什么使用指令会阻止 Angular 使用 Bootstrap 数据切换事件进行推断吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2015-09-02
  • 2019-07-11
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多