-2

In AngularJS I was using $compiler to create HTML elements on the fly during runtime but in Angular 2 + I am not able to find an easy way to create it.

Everyone is recommending dynamically create a component but i don't want to create a component dynamically but i would like to add HTML elements dynamically to an existing component, ngmodel and events will be attached to that HTML elements and that should work properly i.e. events should call the respective function in the existing component.

Whether we can achieve that in Angular 2+ or only in React we can do such stuff?

Dimitar Christoff
  • 26,147
  • 8
  • 50
  • 69
user1892203
  • 479
  • 4
  • 12
  • Can you provide example on React of what you are trying to do? – yurzui Aug 21 '17 at 03:27
  • on button click event, based on user input, I have to decide how many data grid should show to the user. – user1892203 Aug 21 '17 at 04:19
  • 1
    _ngmodel and events will be attached to that HTML elements and that should work properly_ - you should dynamically compile your html as a component. read [creating components on the fly](https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e#2b99). you would have to do the same in AngularJS – Max Koretskyi Aug 21 '17 at 05:22
  • @MaximKoretskyi Thanks a lot. Awesome article, let me try this. – user1892203 Aug 21 '17 at 06:33
  • Please provide information about what exactly you want to create. If you only want to add HTML, but not an Angular component, you don't need to use $compile. – Günter Zöchbauer Aug 21 '17 at 08:46

1 Answers1

0

You can create ANGULAR COMPONENTS at runtime, but not HTML elements. I mean html elements must be some components. Once the component is displayed, you can not modify it's structure. The only thing you can do is "hiding" some elements inside a component and display them when needed , this may be what you need.

sancelot
  • 1,905
  • 12
  • 31