I would like to use Plupload in an Angular2 component and access the Plupload JavaScript file from a CDN. I want it specific to a component so that it is not downloaded if it is not required - I want it to be in a lazy loaded module. How can I do this?
Now fully answered on this page!
The result of this quest, which included offering and awarding bounties to two people who worked hard with me on it, is as follows:
How to use a lazy loaded script in Angular 2
(See edit history for the ugly details that used to make up this question.)
Here's the overview of what you need to do to create a lazy-loaded Plupload feature while loading Plupload from a CDN:
<script>
tag to the page to load the Plupload library from a CDN."plupload is undefined"
error).Complete, working code: https://plnkr.co/edit/4t39Rod4YNAOrHmZdxqc?p=preview
Please take note of the following points in my implementation:
plupload
variable. As long as window.plupload
does not exist, it means the library hasn't been loaded yet and that we should NOT proceed. For simplicity my code just waits for one second and proceeds.document.getElementById('filelist')
). This is something Angular discourages and that you should try avoiding whenever possible. More specifically direct DOM access is used in the following places:
browse_button
config option). For this I could not avoid the direct DOM reference and I used the @ViewChild
decorator to get a hold of the "Select Files" button.fileList
which I display in the template using a standard *ngFor
.Let me know if you have any questions.