event.target无法识别angular2

时间:2016-08-01 13:04:38

标签: angular

我有一个功能

transferDataSuccess(event, cat,label ,e ){
    var target = e.target || e.srcElement || e.currentTarget;
    this.handler.emit( {e:event, cat:cat , labelCS:event.dragData.labelCS} );    
    this.unhighlightElement( event , e );

}

它通过

绑定到元素
(onDropSuccess)="transferDataSuccess($event, node.id , node.label , $event)"

然而它总是导致错误

TypeError: Cannot read property 'nodeName' of undefined

它无法访问event.target,angular2是否有event.target或simmiliar方法?如果我的代码没有问题?为什么它找不到event.target?

//取消强光功能

  unhighlightElement( e , x ){
    e=e||window.event;
    var el =   x || e.target;
    //alert(el.nodeName)
    while( el.nodeName != 'DIV'){
        el = el.parentNode;
    }

    el.style.border="";

    e.stopPropagation();
    e.preventDefault();     
  }

1 个答案:

答案 0 :(得分:1)

我想它应该是:

(onDropSuccess)="transferDataSuccess($event, node.id, node.label)"

,您的功能将如下所示:

transferDataSuccess(event, cat, label){
  this.handler.emit({ e:event, cat:cat, labelCS: event.dragData.labelCS });    
  this.unhighlightElement(event.mouseEvent);
}

unhighlightElement(e){
  var el = e.target;
  //alert(el.nodeName)
  while( el.nodeName != 'DIV'){
    el = el.parentNode;
  }

  el.style.border="";

  e.stopPropagation();
  e.preventDefault();     
}

看到这一行: https://github.com/akserg/ng2-dnd/blob/09296dc1c0a5d7b04ae742d60f49c57398b4e375/src/dnd.droppable.ts#L82

Plunker Example