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;



enter image description here






Oh. It is a pity that it is impossible to have an .html file in static resources. So, .html templates are impossible in lightning.

– hellohowdoyoudo
Sep 9 '18 at 16:41



.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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ