0

I created a non-angular function in my service:

import { Injectable } from '@angular/core';

@Injectable()
export class MyService {
    ...
}

const myFunction = function(){
    alert ( 'test' );
}

And now, I want to call myFunction from the index.html, so I put this in my index.html

<script>setTimeout(function(){ myFunction(); }, 5000);</script>

And I take this error:

Uncaught ReferenceError: myFunction is not defined
    at index.html:15
    at ZoneDelegate.invokeTask (zone.js:414)
    at Zone.runTask (zone.js:181)
    at ZoneTask.invoke (zone.js:476)
    at timer (zone.js:1491)
(anonymous) @ index.html:15
ZoneDelegate.invokeTask @ zone.js:414
Zone.runTask @ zone.js:181
ZoneTask.invoke @ zone.js:476
timer @ zone.js:1491

How I could call myFunction?

George Valentin
  • 618
  • 1
  • 10
  • 21

1 Answers1

0

Add

declare var myFunction;

after the imports.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I added "declare var myFunction: () => void;" because your declaration got an error, but it still same, just the error changed in Uncaught ReferenceError: myFunction is not defined at (index):13 – George Valentin May 08 '17 at 12:01
  • Can you reproduce in Plunker? (Plunker provides a ready-to-use Angular template) – Günter Zöchbauer May 08 '17 at 12:02