Angular 2 指令:意外调用方法或 属性 访问
Angular 2 directive: Unexpected call to method or property access
我正在按照 this 示例在 Angular 2 cli 项目上实现拖放功能。在 Chrome 上一切正常。在 IE 11 上,当我开始拖动元素时出现异常。
关于如何解决这个问题有什么建议吗?
这似乎是 known bug of Internet Explorer caused by calling dataTransfer.setData
with anything but 'Text'. Unfortunately this example was doing exactly that and failed in IE. I have implemented a different approach - using an Angular service to track the current zone. Here is the updated Plunkr。
import { Injectable } from '@angular/core';
@Injectable()
export class DragService {
private zone: string;
startDrag(zone: string) {
this.zone = zone;
}
accepts(zone: string): boolean {
return zone == this.zone;
}
}
可拖动
@Directive({
selector: '[myDraggable]'
})
export class DraggableDirective {
constructor(private dragService: DragService) {
}
@HostBinding('draggable')
get draggable() {
return true;
}
@Input()
set myDraggable(options: DraggableOptions) {
if (options) {
this.options = options;
}
}
private options: DraggableOptions = {};
@HostListener('dragstart', ['$event'])
onDragStart(event) {
const { zone = 'zone', data = {} } = this.options;
this.dragService.startDrag(zone);
event.dataTransfer.setData('Text', JSON.stringify(data));
}
}
放下目标
@Directive({
selector: '[myDropTarget]'
})
export class DropTargetDirective {
constructor(private dragService: DragService) {
}
@Input()
set myDropTarget(options: DropTargetOptions) {
if (options) {
this.options = options;
}
}
@Output('myDrop') drop = new EventEmitter();
private options: DropTargetOptions = {};
@HostListener('dragenter', ['$event'])
@HostListener('dragover', ['$event'])
onDragOver(event) {
const { zone = 'zone' } = this.options;
if (this.dragService.accepts(zone)) {
event.preventDefault();
}
}
@HostListener('drop', ['$event'])
onDrop(event) {
const data = JSON.parse(event.dataTransfer.getData('Text'));
this.drop.next(data);
}
}
我正在按照 this 示例在 Angular 2 cli 项目上实现拖放功能。在 Chrome 上一切正常。在 IE 11 上,当我开始拖动元素时出现异常。
关于如何解决这个问题有什么建议吗?
这似乎是 known bug of Internet Explorer caused by calling dataTransfer.setData
with anything but 'Text'. Unfortunately this example was doing exactly that and failed in IE. I have implemented a different approach - using an Angular service to track the current zone. Here is the updated Plunkr。
import { Injectable } from '@angular/core';
@Injectable()
export class DragService {
private zone: string;
startDrag(zone: string) {
this.zone = zone;
}
accepts(zone: string): boolean {
return zone == this.zone;
}
}
可拖动
@Directive({
selector: '[myDraggable]'
})
export class DraggableDirective {
constructor(private dragService: DragService) {
}
@HostBinding('draggable')
get draggable() {
return true;
}
@Input()
set myDraggable(options: DraggableOptions) {
if (options) {
this.options = options;
}
}
private options: DraggableOptions = {};
@HostListener('dragstart', ['$event'])
onDragStart(event) {
const { zone = 'zone', data = {} } = this.options;
this.dragService.startDrag(zone);
event.dataTransfer.setData('Text', JSON.stringify(data));
}
}
放下目标
@Directive({
selector: '[myDropTarget]'
})
export class DropTargetDirective {
constructor(private dragService: DragService) {
}
@Input()
set myDropTarget(options: DropTargetOptions) {
if (options) {
this.options = options;
}
}
@Output('myDrop') drop = new EventEmitter();
private options: DropTargetOptions = {};
@HostListener('dragenter', ['$event'])
@HostListener('dragover', ['$event'])
onDragOver(event) {
const { zone = 'zone' } = this.options;
if (this.dragService.accepts(zone)) {
event.preventDefault();
}
}
@HostListener('drop', ['$event'])
onDrop(event) {
const data = JSON.parse(event.dataTransfer.getData('Text'));
this.drop.next(data);
}
}