angularjs

Read HTML File into template in Angular 2?  #angularjs #reactjs

  • Stack Overflow works best with JavaScript enabled
  • I also know that my code won’t return the contents of the file, just the string but I haven’t figured out how in Angular2/TypeScript to read in the file contents.
  • When I use the code it seems to treat my entire function as text and so my modal displays the code of my ‘function determineModalContent’ rather than the intended behavior which is to display the contents of the file.
  • And as you can see in this issue on the Angular repo, most probably we won’t get that directive.
  • In the component API (ES6 style), declare a queries entry (or otherwise create your ViewChild using TS), this should map to an element with a template reference variable and so on.

I’m trying to create dynamic modals in Angular 2 using the ng2-bs3-modals. I would like to fill in the modal html body text dynamically based on the parameter passed to the selector used in the main component’s view. When I use this code however it seems to treat my entire function as text and so my modal displays the code of my ‘function determineModalContent’ rather than the intended behavior which is to display the contents of the file.

@ng_real_ninja: Read HTML File into template in Angular 2? #angularjs #reactjs

I’m trying to create dynamic modals in Angular 2 using the ng2-bs3-modals. I would like to fill in the modal html body text dynamically based on the parameter passed to the selector used in the main component’s view. When I use this code however it seems to treat my entire function as text and so my modal displays the code of my ‘function determineModalContent’ rather than the intended behavior which is to display the contents of the file.

I also know that my code won’t return the contents of the file, just the string but I haven’t yet figured out how in Angular2/TypeScript to read in the file contents.

What you need here is something like ng-include I suppose. And as you can see in this issue on the Angular repo, most probably we won’t get that directive. There has been a long discussion whether we need this directive or not, and if not provided how we can implement it by our self.

I tried to make a simple example of how it can be implemented.

For a demo check this Plunker.

directive :

It may be maligned as “verboten” for a number of reasons that I understand, including the underscore you will see, but this works. Fact is I have a need to do this very simply and I don’t want to (nor should I have to) write a lot of code to do it. I can scrub the HTML etc. I get if this is more helpful than an answer but some may find it useful.

In the component API (ES6 style), declare a queries entry (or otherwise create your ViewChild using TS), this should map to an element with a template reference variable and so on. Note that using the queries part of the component API as I show here works fine in TS:

Then in some click handler, onInit or whatever (keeping in mind not to try this too early in the comp lifecycle) get the page you want async, and just dump the contents into the div.

Again, almost surely not “recommended”. But it’s simple and it works. You actually don’t even need the subscribe, you can just set the val in the map func, this is just more complete etc.

When we get an option similar to ng-include (which IMHO is a glaring ommission), or an otherwise three-line option to do this, I’ll make the switch. Handle request errors etc. as you see fit.

angularjs