首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止角JS指令在运动元件时触发两次

防止角JS指令在运动元件时触发两次
EN

Stack Overflow用户
提问于 2014-09-05 18:27:40
回答 1查看 591关注 0票数 0

每次添加元素时,它都会再次触发该指令。我只想将icr-main-modal-placement元素中的elem移动。

代码语言:javascript
复制
.directive('icrMainModal', function() {
  return {
    restrict: 'E',
    templateUrl: 'views/icr-main-modal.html',
    scope: {
      state: '=icrVState'
    },
    transclude: true,
    compile: function(elem, attrs) {

      angular.element('icr-main-modal-placement').append(elem); // because of this, the directive is fired again. I just want to move elem into 'icr-main-modal-placement'

      return function(scope, elem) {

        scope.$on('$destroy', function() {
          return elem.remove();
        });

      };

    }
  };
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-05 18:57:32

我能想到的一种方法是为属性而不是元素创建这个指令,并删除编译/链接方法中的属性?

代码语言:javascript
复制
.directive('icrMainModal', function() {
  return {
    restrict: 'A',
    templateUrl: 'views/icr-main-modal.html',
    scope: {
      state: '=icrMainModal'
    },
    transclude: true,
    compile: function(elem, attrs) {
      elem.removeAttr('icr-main-modal');
      angular.element('icr-main-modal-placement').append(elem); 

      return function(scope, elem) {

        scope.$on('$destroy', function() {
          return elem.remove();
        });

      };
    }
  };
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25691859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档