Instructor: Here we have a simple greeter component which does nothing else than printing out, "Hi there." I then reference this one here in my app component, as you can see here, by using the doGreet tag. That's why we see here in our browser "Hi there" printed out.
Assume we want to insert this tag in a much more dynamic fashion. Let's create here a simple container, which is nothing else than a div. Let's also create a button. Whenever I click that button, I want to insert that greeter tag. Let's create here a method, addGreeter, inside our app component.
Here, we simulate that dynamic insertion by simply leveraging some browser APIs. I am having here an instance of my container by using the document.getElementById. On that container, I am using the innerHTML. I'm inserting here my doGreet tag.
Let's try this out and click the button. You can see actually nothing happens. Let's inspect our elements tab here. We see our container elements. We also see the doGreet tag being inserted properly. Our code actually works.
The problem is that Angular doesn't recognize the doGreet tag because it has been inserted into the template in a dynamic fashion. Right now, Angular needs all of the elements to be present in the template or to use the dynamic component factory to instantiate it at runtime via your code.
The whole flexibility Angular Elements gives us is actually to do something like this by inserting a tag dynamically. Then Angular will take care of instantiating that component on the fly.
This can be very useful when, for instance, you have a server-side CMS system which gives you back already a pre-rendered HTML piece which you want to just insert into your whole document.
Let's see how to leverage Angular Elements to achieve just that. First of all, we need to install the Angular Elements. We can do that by the ng add command.
Once the installation is done, we can go to the package.json and inspect what has been inserted by the Angular add command. Here, we see the Angular Elements library, which has been added to our package.json. You might have different versions here, depending on the Angular version you are currently using. Also inserted here a document-register-element polyfill.
If we go to the angular.json file, we should see there a similar script being registered for our app which we are starting. Once we have that installed, let's try and instantiate our Angular Elements. If we go to the app module, we see there the app component and greeter component being registered.
Whenever we insert something dynamically, we also have to pass it to the entry components. Let's add here our greeter component. Furthermore, we can leverage the Angular Elements API to actually create an Angular element out of our greeter component.
In the constructor of our app module, we create here an element. Then we import the createCustomElement function from the Angular Elements package we just installed. Now, we can directly use that below here.
We pass here the type of our greeter component, as well as we have to give it the injector which it will use internally for resolving the dependencies via the dependencies injector.
In order to be able to pass it in, we need to also get it here in our constructor. We can say something like "injector = injector." We auto-import that here from angular/core. We can just pass it along like this.
With that, we have created here an instance of a custom element out of our Angular component. Now we use the Custom Elements API, which is a native browser API to actually define its tag. Let's call it doGreet and give it here the element.
What we can also do since we define the tag here, in order to prevent people from using this component directly because now it should be used via the Custom Element API, we can comment here the selector tag.
Let's go back. Let's save also our app module. Let's restart here our application. Let's go to the browser and have a refresh there as well. If we click, we see still nothing happens. There are some errors here in the console. Let's inspect them.
If you get such an error such as here, "Please use the new operator. This DOM object constructor cannot be called as a function," then you might run into a problem of the polyfills. As we have seen, Angular Elements installs here that document-register-element polyfill.
There are various ways to circumvent this problem. First of all, we can go to the tsconfig file. Rather than having as a target ES5, we can target ES2015 in case we are running evergreen browsers.
Let's restart here our compilation process. Once it's up and running, let's click that button. We can see now it gets inserted properly. Our custom element here gets instantiated. We also see that this doGreet tag now has a version attached to it, which means that this is a separate Angular application if you want inside the existing Angular app.
Since compiling as a target to ES2015 might not be an option yet, what you can do alternatively is to install an auto-polyfill. Let's stop here our compilation process. Let's add here the Web Components webcomponentsjs polyfill. I'm installing it with yarn. You can do as well as an npm install.
Once installation finishes, we go to our polyfills file, which is inside our project. At the very end here, we import the Web Components webcomponentsjs/custom-elements-es5-adapter.js.
Let's save this. Let's restart the Angular CLI. Once the compilation succeeds, let's refresh again our browser. Now if we reclick that addGreeter, we see again this works just as we expect. Also, if we go down to our elements panel, we can open our container. We see our doGreet component has been instantiated properly.