You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@tbosch with the latest build, how can we bootstrap with custom selector?
ComponentResolver is deprecated for dynamic compilation. Use ComponentFactoryResolver together with @AppModule/@Component.precompile or ANALYZE_FOR_PRECOMPILE provider instead.
@tbosch would it be possible to forward paremeters into the constructor? I only know the elements I want to initialize the component on at runtime (html is fetched via ajax). Probably call something like: platformBrowserDynamic().bootstrapModule(MainModule, {selector: 'someSelector'});
when the html is in the dom and then use the 'selector' parameter in the constructor, as you described.
From what I can see right now boostrapModule does not allow to forward parameters to the module constructor, is that correct? Would you know a way to work around this until there is an API for this?
@tboschhttps://github.com/tbosch would it be possible to forward
paremeters into the constructor? I only know the elements I want to
initialize the component on at runtime (html is fetched via ajax). Probably
call something like:
platformBrowserDynamic().bootstrapModule(MainModule, {selector:
'someSelector'});
when the html is in the dom and then use the 'selector' parameter in the
constructor, as you described.
From what I can see right now boostrapModule does not allow to forward
parameters to the module constructor, is that correct? Would you know a way
to work around this until there is an API for this?
@tbosch thanks for the pointers, after digesting your answer I noticed that I didn't even need a custom selector when using the approach you described!
@tbosch Out of curiosity, you snippet worked fine in the constructor of the module. However when I put it into ngOnBootstrap of the module, it didn't start the component lifecycle (breakpoint in ngOnInit of the comonent). I also tried it as a separate method to dynamically attach components to the DOM, are there any additional steps required to start the component?
Hey @tbosch, following @playground question about custom selectors, I have created a code to dynamically bootstrap modules in my page (we have a custom set up that lazy load the components as they apear in the screen).
This code works fine and do the task, but I have a problem, when I include an NgModule in the imports of one of the bootstrapped modules, the injector doesn't recognize the services declared in the imported module. Only if I include the NgModule (the one with a service) in the root module, it gets injected correctly.
Here's a summary of the code that bootstraps with custom selector
Here's a plnkr with the example working (NgModule imported in the root module)
Here's a plnkr with the example failing because a missing provider (NgModule imported in the dynamically bootstrapped modules)
Here's a plnkr working correctly without lazy-load with custom selector
Do you have any ideas on what might be missing for the injector to recognize the services?
Could you create a separate issue for this? This is in general just related to module loading, and not to bootstrap of a component with a custom selector.
Your problem is that a service in a root module can not inject a service that is declared in a lazy loaded module, only the other way around. And this makes sense, as loading of modules is intended for code loading, i.e. your root module cannot refer to the service of the lazy loaded module as that code has not been loaded on bootstrap.
Incorporating the following code snippet as you suggested with Angular 2.0.0, the components are getting bootstrapped and lazy loaded, however, they are not getting rendered as if change detection didn't kick in probably due to race condition. If we explicitly fire changeDetectorRef.detectChanges() in the console, then the components show up. Is the nice api for this available yet?
public bootstrapWithCustomSelector(moduleFactory: NgModuleFactory,
selector: string,
ngClassName: string
): void {
// Get the parent injector and create the module
const parentInjector = ReflectiveInjector.resolveAndCreate([], this.appModule.injector);
const ngModule = moduleFactory.create(parentInjector);
// Some dependencies from this module
const appRef = ngModule.injector.get(ApplicationRef);
const inj = ngModule.injector.get(Injector);
const lazyLoad = ngModule.injector.get(WuLazyLoadConfig, {component: null});
if (!lazyLoad.component) {
throw(${ngClassName} is not using the WuLazyLoadModule. This is neccesary for bootstrap the component. Check the docs.);
}
// Get the component factory and create it
const compFactory = ngModule.componentFactoryResolver
.resolveComponentFactory(lazyLoad.component);
const compRef = compFactory.create(inj, [], selector);
// Register the change detector to the app and trigger the first detection
appRef.registerChangeDetector(compRef.changeDetectorRef);
compRef.changeDetectorRef.detectChanges();
}
angular-automatic-lock-bot commented on Sep 9, 2019
Activity
b-strauss commentedon Jul 3, 2016
Could this be used to bundle css that should only be usable and scoped to components inside an app module? For example, commonly used flex styles.
tbosch commentedon Jul 6, 2016
@b-strauss Sorry, that is not planned yet.
9 remaining items
playground commentedon Jul 12, 2016
@tbosch with the latest build, how can we bootstrap with custom selector?
ComponentResolver is deprecated for dynamic compilation. Use ComponentFactoryResolver together with @AppModule/@Component.precompile or ANALYZE_FOR_PRECOMPILE provider instead.
tbosch commentedon Jul 26, 2016
We don't have a nice API for this yet, sorry.
But you can do the following, please read this doc (http://g.co/ng/modules) to understand the concepts before.
I.e.
entryComponentComponentFactoryfrom theComponentFactoryResolverApplicationRefvia the private APIApplicationRef.registerChangeDetectorso that the component gets dirty checkedtom-mayer commentedon Aug 30, 2016
@tbosch would it be possible to forward paremeters into the constructor? I only know the elements I want to initialize the component on at runtime (html is fetched via ajax). Probably call something like:
platformBrowserDynamic().bootstrapModule(MainModule, {selector: 'someSelector'});when the html is in the dom and then use the 'selector' parameter in the constructor, as you described.
From what I can see right now boostrapModule does not allow to forward parameters to the module constructor, is that correct? Would you know a way to work around this until there is an API for this?
tbosch commentedon Aug 30, 2016
you can always store data in a top level variable in a module...
On Tue, Aug 30, 2016 at 9:17 AM Tom Mayer [email protected] wrote:
tom-mayer commentedon Aug 31, 2016
@tbosch thanks for the pointers, after digesting your answer I noticed that I didn't even need a custom selector when using the approach you described!
For some context, I tried to instantiate multiple instances of the same root component.
http://stackoverflow.com/questions/39231943/multiple-instances-of-the-same-root-application-in-angular-2
tom-mayer commentedon Sep 1, 2016
@tbosch Out of curiosity, you snippet worked fine in the constructor of the module. However when I put it into ngOnBootstrap of the module, it didn't start the component lifecycle (breakpoint in ngOnInit of the comonent). I also tried it as a separate method to dynamically attach components to the DOM, are there any additional steps required to start the component?
tbosch commentedon Sep 2, 2016
Not that I know of...
tom-mayer commentedon Sep 2, 2016
Interesting, if I wrap it into a zone.run in ngDoBootstrap it works just fine. A well,... thanks for the great work so far on angular2 !
diestrin commentedon Sep 6, 2016
Hey @tbosch, following @playground question about custom selectors, I have created a code to dynamically bootstrap modules in my page (we have a custom set up that lazy load the components as they apear in the screen).
This code works fine and do the task, but I have a problem, when I include an NgModule in the
importsof one of the bootstrapped modules, the injector doesn't recognize the services declared in the imported module. Only if I include the NgModule (the one with a service) in the root module, it gets injected correctly.Here's a summary of the code that bootstraps with custom selector
Here's a plnkr with the example working (NgModule imported in the root module)
Here's a plnkr with the example failing because a missing provider (NgModule imported in the dynamically bootstrapped modules)
Here's a plnkr working correctly without lazy-load with custom selector
Do you have any ideas on what might be missing for the injector to recognize the services?
tbosch commentedon Sep 6, 2016
Could you create a separate issue for this? This is in general just related to module loading, and not to bootstrap of a component with a custom selector.
Your problem is that a service in a root module can not inject a service that is declared in a lazy loaded module, only the other way around. And this makes sense, as loading of modules is intended for code loading, i.e. your root module cannot refer to the service of the lazy loaded module as that code has not been loaded on bootstrap.
diestrin commentedon Sep 6, 2016
Moving, sorry...
playground commentedon Sep 19, 2016
Hi @tbosch ,
Incorporating the following code snippet as you suggested with Angular 2.0.0, the components are getting bootstrapped and lazy loaded, however, they are not getting rendered as if change detection didn't kick in probably due to race condition. If we explicitly fire changeDetectorRef.detectChanges() in the console, then the components show up. Is the nice api for this available yet?
public bootstrapWithCustomSelector(moduleFactory: NgModuleFactory,
selector: string,
ngClassName: string
): void {
// Get the parent injector and create the module
const parentInjector = ReflectiveInjector.resolveAndCreate([], this.appModule.injector);
const ngModule = moduleFactory.create(parentInjector);
// Some dependencies from this module
const appRef = ngModule.injector.get(ApplicationRef);
const inj = ngModule.injector.get(Injector);
const lazyLoad = ngModule.injector.get(WuLazyLoadConfig, {component: null});
if (!lazyLoad.component) {
throw(
${ngClassName} is not using the WuLazyLoadModule. This is neccesary for bootstrap the component. Check the docs.);}
// Get the component factory and create it
const compFactory = ngModule.componentFactoryResolver
.resolveComponentFactory(lazyLoad.component);
const compRef = compFactory.create(inj, [], selector);
// Register the change detector to the app and trigger the first detection
appRef.registerChangeDetector(compRef.changeDetectorRef);
compRef.changeDetectorRef.detectChanges();
}
angular-automatic-lock-bot commentedon Sep 9, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.