Instructor: The main idea behind compiling down Angular components into custom elements, and being able to use them outside of Angular as a single script file include is usually because you want to fix them into existing applications, or you might even want to build some kind of micro front end where you have React application, maybe even Vue, Jest, and Angular components managed by some vanilla Jest router, for instance.
In those use cases, these custom elements, these web components, should be as independent as possible, so they fulfill certain functionality. Then they communicate via inputs and outputs with the other part of the application, which then might be vanilla Jest, or even a React application.
Now, if we take a look here, we see our share service. We see the feedback form component. We also see that newly generated component, that display component. Now, first of all, what we want to have here is we want in the display component, just visualize the name that gets entered here by the user and submitted.
We go to that HTML part here, and we can directly use that share service. Let's call it like this, shareService.name. In the share service, we will have a property -- for now, let's just call it here name -- which will be set by our feedback form component.
In the feedback form component, in the constructor, let's import that share service. This is a normal sharing state over an Angular service, just as you would do in a normal Angular application. We call shareService.name equals, and say value.name, which is here, that input form, which is here, returned by this form control. Let's save this.
The display component, we also need to make sure that we get here a reference to that share service. We need to make it public, such that it can be accessed here directly in our template, and display the name.
Great. Now, we have wired up some components, which use the same service, which is a singleton class. Thus, can set and greet property names from that service. Now, we also need here to register a new web component, a new custom Angular element.
We have our display component, which should also go here to those entry components. We also want to register it as a custom element. We then need to adjust, of course, here our name. Let's call it display component.
We also need to pass in here our display element. Now, since this is side bootstrapping Angular element, we can also move that whole logic down here into that ngDoBootstrap. It works also in the constructor, but it's probably more clean to place it down here, as this is where everything gets bootstrapped.
Now, let's just adjust here the code. We need to use the dist here, since we're referencing now the injector, which gets passed in here via the constructor. Now, once we have that, let's build these web components.
Once the compilation finishes, we can go to our index.html file here, can remove this part here, because we don't need it anymore. Let's place here some other HTML text inside. Then we can use our display component.
Let's save this. Now, you see here, this gets already precompiled, because we pass in the input here via that HTML attribute. Now, if I submit, we can see that we have our other component below here, which is our display component.
Which gets this state shared over that service, which we have just built. Whenever I change here .name, and submit it, it will be set on the service, just as we've seen before. This one will automatically update. What you can see here is now, effectively having multiple root-level components for our Angular app, which is normally not possible.