How can I access the html text in static resource file in lightning client-side controller?
How can I access the html text in static resource file in lightning client-side controller?
How can I access the html text in static resource file in lightning client-side controller?
I know how to upload .html file to static resources and how to acquire it using <ltng:require>, but I do not know how to access its content once it is acquired.
.html
<ltng:require>
1 Answer
1
<ltng:require> is used for loading JavaScript and style sheets, not static HTML. In order to make HTML content available for your Lightning JavaScript controller, you'd need to structure your static resource as a JavaScript file that assigns HTML content to some top-level property, which you can then access in your controller and include in your component.
<ltng:require>
Alternately, you'd need to call out to your server-side Apex controller to query for the body of the StaticResource and return the data to you.
StaticResource
Here's an example of the former approach.
testsr
window.myHTML = '<strong>Hello, <em>world!</em></strong>';
TestQ231776.app
<aura:application >
<aura:attribute type="String" name="html" />
<ltng:require scripts="! $Resource.testsr " afterScriptsLoaded="!c.afterScriptsLoaded" />
<p>
<aura:unescapedHtml value="! v.html " />
</p>
</aura:application>
TestQ231776Controller.js
(
afterScriptsLoaded : function(component, event, helper)
component.set('v.html', window.myHTML);
)
TestQ231776.css
.THIS strong
font-weight: bold;
.THIS em
font-style: italic;

.html
.html
Well, that's overstating it a little. There are (at least) two routes to access static HTML, as above. But remember also that a Lightning component is itself something like a template. Your Lightning component markup is HTML + Lightning components.
– David Reed
Sep 9 '18 at 17:06
Thanks for contributing an answer to Salesforce Stack Exchange!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Oh. It is a pity that it is impossible to have an
.htmlfile in static resources. So,.htmltemplates are impossible in lightning.– hellohowdoyoudo
Sep 9 '18 at 16:41