2

I want to load a function from external script and then use it in the component controller:

example of a function that i tried to call from the component controller (from the Init method specifically)

 function myFunction() {
    log.console('Hiiii'); 
} 

I loaded the script like that:

<ltng:require scripts="{!$Resource.DataArrays}"
    afterScriptsLoaded="{!c.doInit}" />

then i tried simply to call it from the Init method:

    doInit : function(cmp){

  myFunction();
}

It does not work, i got an error like this one:

Uncaught Action failed: c:realEstateFilterComponent$controller$doInit [myFunction is not defined]

Do you know to make it work ?

9
  • Did you try window.myFunction(); inside the doInit method?
    – javanoob
    Commented Mar 5, 2017 at 16:58
  • Yes i used Window.myFunction, when i specify another function than DoInit() in afterScriptsLoaded , it works.
    – user28829
    Commented Mar 5, 2017 at 17:06
  • 2
    In your resource file do like this : window.myFunction = function (){..} then try as mentioned in my first comment..it will work. I just tried on my end.
    – javanoob
    Commented Mar 5, 2017 at 17:27
  • 1
    It is the same concept.. Whatever functions we want to invoke that are present inside the static resource need to be attached to window object to be able to call them from lightning controller functions.
    – javanoob
    Commented Mar 5, 2017 at 17:35
  • 1
    May be you have line like this in your component? <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> If yes, then resource is not loaded by then.
    – javanoob
    Commented Mar 5, 2017 at 17:42

0

You must log in to answer this question.

Browse other questions tagged .