Add option to specify view providers when dynamically creating components

This issue has been created since 2022-11-24.

Which @angular/* package(s) are relevant/related to the feature request?

core

Description

Let's say our component has a child directive with dependency decorated by @Host. And we want to specify its provider.

You can do that in the compile time using viewProviders option in @Component decorator. But, currently, there is no way to do it programmatically.

i.e. when that component is created dynamically by ViewContainerRef.createComponent() method, you can only specify providers using injector option in Injector.create() method. But those providers will not be applied to that directive.

Here is live example. Take a closer look at providers and console output.

When providers array is used, factory is not called because search stops on <input> component (ngModel uses @Host and @Optional decorators):

@Component({
  selector: 'app-provider-form',
  template: '<input name="username" [(ngModel)]="username" />',
  providers: [{provide: ControlContainer, useFactory: () => {console.log('provider factory')}}],
})
export class ProviderestFormComponent  {
  username: string;
}

But when viewProviders array is used factory is called:

@Component({
  selector: 'app-view-provider-form',
  template: '<input name="username" [(ngModel)]="username" />',
  viewProviders: [{provide: ControlContainer, useFactory: () => {console.log('view provider factory')}}],
})
export class ViewProviderFormComponent  {
  username: string;
}

So it would be really convenient to be able to specify this when form is created dynamically. Otherwise there is no easy way to bind parent ngForm directive and child ngModels.

Related issues: #47580 , #34780

Proposed solution

Add viewInjector option to ViewContainerRef.createComponent() options. Or maybe add viewProviders key to Injector.create().

Alternatives considered

n/a

More Details About Repo
Owner Name angular
Repo Name angular
Full Name angular/angular
Language TypeScript
Created Date 2014-09-18
Updated Date 2022-12-01
Star Count 85220
Watcher Count 3042
Fork Count 22607
Issue Count 1316

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Google Cloud as a provider 0 2021-06-29 2022-11-01
Proper way to handle secrets when deploying the site 0 2022-09-28 2022-10-10
Handling node page updates? 6 2022-09-28 2022-10-22
Need Notify Code that Connected External smtp server 1 2021-02-09 2022-11-06
Source Data endpoints not available 0 2022-02-14 2022-11-01
Screen reader is not announcing status message "No emojis found" after searching for emoji by typing letters in "Search emojis" edit field. 3 2021-06-01 2022-11-23
[Bug] Ctrl+S / Cmd+S shortcut doesn't work when inside select2 inputs 0 2022-02-02 2022-07-13
Google Wavenet TTS invalid Argument 2 2021-03-14 2022-10-29
How can I use ubertooth-one for getting the MAC and name of the nearby bluetooth device? 1 2019-11-06 2022-10-07
Text input field not cleared after adding subdomain 0 2021-11-14 2022-02-15
AttributeError: 'Document' object has no attribute '_getXrefString' 3 2021-11-13 2022-11-19
Can't save project, can't uninstall, can't reinstall, freezes entire computer. 2 2021-09-16 2022-11-04
Enable jit-analyze to take multiple metrics as input and add an option to output .md file 1 2021-01-22 2022-11-30
Trao 里面接入的时候无法从 getApp()里面获取到sensors 1 2020-09-03 2022-11-26
dr_wav (dev): Coverity flagged Untrusted loop bound (CWE-606) in `drwav_init__internal` 2 2021-12-07 2022-11-21
Minor Visual bug in Claiming a Pull payment, weird bullet point by providing invalid address 3 2022-03-28 2022-10-13
More of a feature request 0 2022-09-25 2022-11-10
trec-car-tools-java dependency Error 11 2022-03-01 2022-10-28
Multivalue Logic System 9-value support 2 2021-09-02 2022-10-31
ScrollTrigger pinned element position issues with content visibility 1 2021-08-03 2022-11-15
destructive decode of pathname 0 2021-05-03 2022-11-29
How do I use the batch editor? 3 2021-09-06 2022-10-15
D3DCompile error with vector indexing 4 2022-01-27 2022-11-24
About the `whl` file name generated by `python setup.py bdist` on Windows 0 2021-12-07 2021-12-09
SurveyJS production build not working with CSS optimization 6 2021-10-28 2022-02-08
[Task]: Revamp of Datasource creation flow Logic 4 2022-01-21 2022-11-03
Parse error on default constraints (...type) 5 2021-07-03 2022-10-19
关于序言的翻译(留作存档) 0 2021-10-15 2021-12-31
Bringing Euterpea up to date 5 2022-10-09 2022-11-11
www.euterpea.com is down. 8 2022-10-09 2022-11-11
ConfigurationTest does not clean up environment variables 0 2021-10-15 2022-11-18
Export .tflite 6 2021-10-11 2022-01-17
[WiiU] Core crashes whenever I try to choose a game 3 2021-10-22 2022-11-09
[Yang] Failed to find the xDbSpecMap: xpath for Annotation yang with field-name and table-name 1 2022-04-25 2022-09-19
Bump jinja2 from 2.7.3 to 3.0.0 1 2021-05-12 2022-02-08
`bloop help` (as well as other commands) hangs on Mac M1 - unable to connect to server in metals 6 2022-06-21 2022-11-07
sign reference filesystems 0 2022-01-07 2022-11-19
Missing link from Asset to owner Item after signing STAC Item 5 2021-09-02 2022-11-19
The dialog after pressing the close button can't be tabbed so that you can close the window using only the keyboard. 2 2022-09-01 2022-10-08
(CI) Ray Load Tests Need Default AWS Region Configured to Run Tests 1 2022-08-08 2022-10-15
Confusing "Permission Denied" error message when ~/.config/borg is unreadable 4 2021-08-08 2022-09-28
Video watching minutes 1 2022-09-26 2022-11-19
`error[internal]: left behind trailing whitespace` 2 2021-10-30 2022-07-16
Documentation for Winget search is not correct 4 2022-08-24 2022-11-08
VS Code Source Control does not recognise the git repo. 5 2022-07-20 2022-11-22
Sprite loading performance 11 2021-09-16 2022-11-16
Database seeder output is messed up by new Artisan look 7 2022-08-02 2022-08-02
When extension is enabled, it is locking folders 3 2021-05-17 2022-11-10
bug(cdk/schematics): ng update migration does not find the global stylesheet. 0 2022-01-12 2022-11-30
Incomplete generic types in TypeScript 2 2017-07-31 2022-11-27