Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprinkling Angular 2 components inside a non-angular page

Tags:

Currently looking at upgrade paths from Angular 1 -> Angular 2 and one things we've done with our Angular 1 work is reuse some of our components on public facing non-app pages.

These pages are effectively static HTML (though they are rendered by Rails) and then some Angular 2 components are dropped into the page in places. This worked from with Angular 1, we simply bootstrapped the document element with a module that provided the directives and components we needed. There is no routing at all.

With Angular 2 it looks like it is all or nothing. You declare a single root component and everything is rendered through that. This would be a big shift for us and I'd like to avoid changing how we are doing things on these public facing pages.

Is it possible at all to just use Angular 2 components as needed in static HTML pages or will we need to move to a single root element SPA design?

In a nutshell, what I'm asking is if it is possible to have a mix of static content with dynamic angular components sprinkled within, or must all angular components live within a single root element on the page?

like image 417
Chris Nicola Avatar asked Feb 15 '16 20:02

Chris Nicola


People also ask

How does Angular integrate two components?

app/hero-detail.component.ts (initial version)To define a component, you always import the Component symbol. The @Component decorator provides the Angular metadata for the component. The CSS selector name, hero-detail , will match the element tag that identifies this component within a parent component's template.

Can you reuse Angular components in another project?

To get started, go ahead and install bit-cli, then head over to the project from which to share the components, and initialize a bit workspace. Then, head over to bit. dev and create a free account. Then, create a collection to host your shared components.

What is an Angular 2 component?

Components are a logical piece of code for Angular JS application. A Component consists of the following − Template − This is used to render the view for the application. This contains the HTML that needs to be rendered in the application. This part also includes the binding and directives.

What is @component in Angular?

Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components. Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated for a given element in a template.


1 Answers

So this is simpler than I originally thought. In the Angular 2 docs it has some specific wording around bootstrapping multiple apps.

Bootstrapping Multiple Applications

When working within a browser window, there are many singleton resources: cookies, title, location, and others. Angular services that represent these resources must likewise be shared across all Angular applications that occupy the same browser window. For this reason, Angular creates exactly one global platform object which stores all shared services, and each angular application injector has the platform injector as its parent.

Each application has its own private injector as well. When there are multiple applications on a page, Angular treats each application injector's services as private to that application.

So it seems clear that this is intended to be possible and that multiple apps share service resources which is what I would hope for.

I've done some trivial tests with multiple bootstrapped components and it works fine. One thing I have not yet tried is bootstrapping an Angular 2 attribute directive for use outside of Angular 2 components. I suspect that won't work and that bootstrap only works with Components and not Directives.

In terms of guidance, I would suggest that Angular 2 is not really designed for sprinkling behaviour throughout a static page and probably should not be used that way. Rather, while you may have multiple sections of your paged defined by multiple apps, that components should make up nearly all of the document/interface.

like image 54
Chris Nicola Avatar answered Sep 21 '22 20:09

Chris Nicola