0

I have an angular application where I need to explicitly fire a drag event. I am using AOT - ngc and rollup.

    let event1 = new DragEvent();
event1.initDragEvent('dragstart', true, true, null, null, null, null, null, null, null, null, null, null, null, null, null);
this._renderer.invokeElementMethod(this._elementRef.nativeElement, 'dispatchEvent', [event1]);
        

The code compiles fine and the page loads in the browser. But at runtime, I get this error:

Failed to construct 'DragEvent': 1 argument required, but only 0 present.

Now, I change the code to look like this:

let event1 = new DragEvent('dragstart');
this._renderer.invokeElementMethod(this._elementRef.nativeElement, 'dispatchEvent', [event1]);

Now, the compiler throws and error saying:

Expected 0 arguments, but got 1.

How do I overcome this problem?

Shilpa Nagavara
  • 1,055
  • 2
  • 16
  • 31

2 Answers2

1

Try using,

this._renderer.invokeElementMethod(this._elementRef.nativeElement, 'dispatchEvent', [new DragEvent('dragstart', true, true)]);
Sahil Gupta
  • 73
  • 1
  • 3
  • 12
  • This didn't work. Error ==> Expected 0 arguments, but got 3. – Shilpa Nagavara Jun 14 '17 at 14:11
  • Try initializing event using: `object.createEvent (event1 );` rather than using : `new DragEvent();` – Sahil Gupta Jun 15 '17 at 06:34
  • I am not sure what you mean. What is object - the native element? What should event1 be instantiated to? Can you please provide a couple of lines of code? – Shilpa Nagavara Jun 15 '17 at 06:41
  • `event1 = new DragEvent(); event1 .createEvent (DragEvent); event1 .initDragEvent ('dragstart', true, true, null, null, null, null, null, null, null, null, null, null, null, null, null); this._renderer.invokeElementMethod(this._elementRef.nativeElement, 'dispatchEvent', [event1]);` – Sahil Gupta Jun 15 '17 at 07:50
  • This didn't work. Same error as the original fish. I think if I use the right type file, it would help. Would you know which defintely typed component contains definition for drag event? – Shilpa Nagavara Jun 16 '17 at 06:12
0

The answer was rather simple.

let event1 = new Event('dragstart');
Shilpa Nagavara
  • 1,055
  • 2
  • 16
  • 31