0

I have a static resource named as 'geoCode', which is a javascript file, containing a function. I want to call this function from lightning component controller and receive an alert message on screen.

function callMe() {
    alert('You are inside static resource');
}
4

You should be able to do this right inside of your controller code. However, you need to make sure that the call to this function doesn't happen until the script is loaded. This is done by specifying a controller action to be called in the afterScriptsLoaded parameter of <ltng:require>.

In your component:

<ltng:require scripts="/resource/geoCode" afterScriptsLoaded="{!c.scriptsLoaded}"/>

In your controller:

({
  scriptsLoaded : function(component, event, helper) {
    callMe();
  }
})

See this for the official docs: Using External JavaScript Libraries with Lightning Components

| improve this answer | |
  • Its giving error as below: Uncaught error in markup://ltng:afterScriptsLoaded : callMe is not defined I have exactly what you said. Actually I even copy pasted your code. then also same error. – Ayan Sarkar Sep 15 '15 at 10:10
0

I think you need to add one more thing to your component definition:

<ltng:require scripts="/resource/geoCode/YOURFILENAME" afterScriptsLoaded="{!c.scriptsLoaded}"/>

This is because you have named the resource (this is the name you called it on upload) but you have not specifically referenced the actual file (remember to leave off the extension if it's a single file)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.