【问题标题】:Angular: ignore messages emitted by selfAngular:忽略自我发出的消息
【发布时间】:2015-01-22 23:04:24
【问题描述】:

我有一个扩展和关闭 div 高度的指令。它使用了多次,所以当用户打开一个盒子时,我想关闭其他打开的盒子。我认为最好发出一个事件,但问题是指令随后也会自行关闭,因此无法打开任何 div。

我可以忽略来自同一指令的消息吗?

angular.module('cinemaApp')
.directive('expand', function ($rootScope) {
 return {
  restrict: 'A',
  link: function postLink(scope, element, attrs) {
    $rootScope.$on('contract', function(payload) {
      element.css('height', attrs.fixedheight);
    });

    scope.$watch('open', function(value) {
        element.css('height', (value ? 'auto' : attrs.fixedheight));

      $rootScope.$emit('contract');
    });
    element.css('height', attrs.fixedheight);
  }
 };
});

【问题讨论】:

    标签: javascript angularjs events dom-events


    【解决方案1】:

    当您发出事件时,您可以将额外的参数传递给监听器。所以你可以发出像

    $rootScope.$emit('contract', element);
    

    然后像这样听

    $rootScope.$on('contract', function(event, target) {
      if (target != element)
        element.css('height', attrs.fixedheight);
    });
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多