NgComponentOutlet

Instantiates a single Componenttype and inserts its Host View into current View. NgComponentOutlet provides a declarative approach for dynamic component creation.

查看"说明"...

NgModule

选择器

属性

属性说明
@Input()
ngComponentOutlet: Type<any>
@Input()
ngComponentOutletInjector: Injector
@Input()
ngComponentOutletContent: any[][]
@Input()
ngComponentOutletNgModuleFactory: NgModuleFactory<any>

说明

NgComponentOutlet requires a component type, if a falsy value is set the view will clear and any existing component will get destroyed.

Fine tune control

You can control the component creation process by using the following optional attributes:

Syntax

Simple

<ng-container *ngComponentOutlet="componentTypeExpression"></ng-container>
      
      <ng-container *ngComponentOutlet="componentTypeExpression"></ng-container>
    

Customized injector/content

<ng-container *ngComponentOutlet="componentTypeExpression; injector: injectorExpression; content: contentNodesExpression;"> </ng-container>
      
      <ng-container *ngComponentOutlet="componentTypeExpression;
                                  injector: injectorExpression;
                                  content: contentNodesExpression;">
</ng-container>
    

Customized ngModuleFactory

<ng-container *ngComponentOutlet="componentTypeExpression; ngModuleFactory: moduleFactory;"> </ng-container>
      
      <ng-container *ngComponentOutlet="componentTypeExpression;
                                  ngModuleFactory: moduleFactory;">
</ng-container>
    

A simple example

@Component({selector: 'hello-world', template: 'Hello World!'}) class HelloWorld { } @Component({ selector: 'ng-component-outlet-simple-example', template: `<ng-container *ngComponentOutlet="HelloWorld"></ng-container>` }) class NgTemplateOutletSimpleExample { // This field is necessary to expose HelloWorld to the template. HelloWorld = HelloWorld; }
      
      
  1. @Component({selector: 'hello-world', template: 'Hello World!'})
  2. class HelloWorld {
  3. }
  4.  
  5. @Component({
  6. selector: 'ng-component-outlet-simple-example',
  7. template: `<ng-container *ngComponentOutlet="HelloWorld"></ng-container>`
  8. })
  9. class NgTemplateOutletSimpleExample {
  10. // This field is necessary to expose HelloWorld to the template.
  11. HelloWorld = HelloWorld;
  12. }

A more complete example with additional options:

@Injectable() class Greeter { suffix = '!'; } @Component({ selector: 'complete-component', template: `Complete: <ng-content></ng-content> <ng-content></ng-content>{{ greeter.suffix }}` }) class CompleteComponent { constructor(public greeter: Greeter) {} } @Component({ selector: 'ng-component-outlet-complete-example', template: ` <ng-container *ngComponentOutlet="CompleteComponent; injector: myInjector; content: myContent"></ng-container>` }) class NgTemplateOutletCompleteExample { // This field is necessary to expose CompleteComponent to the template. CompleteComponent = CompleteComponent; myInjector: Injector; myContent = [[document.createTextNode('Ahoj')], [document.createTextNode('Svet')]]; constructor(injector: Injector) { this.myInjector = ReflectiveInjector.resolveAndCreate([Greeter], injector); } }
      
      
  1. @Injectable()
  2. class Greeter {
  3. suffix = '!';
  4. }
  5.  
  6. @Component({
  7. selector: 'complete-component',
  8. template: `Complete: <ng-content></ng-content> <ng-content></ng-content>{{ greeter.suffix }}`
  9. })
  10. class CompleteComponent {
  11. constructor(public greeter: Greeter) {}
  12. }
  13.  
  14. @Component({
  15. selector: 'ng-component-outlet-complete-example',
  16. template: `
  17. <ng-container *ngComponentOutlet="CompleteComponent;
  18. injector: myInjector;
  19. content: myContent"></ng-container>`
  20. })
  21. class NgTemplateOutletCompleteExample {
  22. // This field is necessary to expose CompleteComponent to the template.
  23. CompleteComponent = CompleteComponent;
  24. myInjector: Injector;
  25.  
  26. myContent = [[document.createTextNode('Ahoj')], [document.createTextNode('Svet')]];
  27.  
  28. constructor(injector: Injector) {
  29. this.myInjector = ReflectiveInjector.resolveAndCreate([Greeter], injector);
  30. }
  31. }

A more complete example with ngModuleFactory:

@Component({selector: 'other-module-component', template: `Other Module Component!`}) class OtherModuleComponent { } @Component({ selector: 'ng-component-outlet-other-module-example', template: ` <ng-container *ngComponentOutlet="OtherModuleComponent; ngModuleFactory: myModule;"></ng-container>` }) class NgTemplateOutletOtherModuleExample { // This field is necessary to expose OtherModuleComponent to the template. OtherModuleComponent = OtherModuleComponent; myModule: NgModuleFactory<any>; constructor(compiler: Compiler) { this.myModule = compiler.compileModuleSync(OtherModule); } }
      
      
  1. @Component({selector: 'other-module-component', template: `Other Module Component!`})
  2. class OtherModuleComponent {
  3. }
  4.  
  5. @Component({
  6. selector: 'ng-component-outlet-other-module-example',
  7. template: `
  8. <ng-container *ngComponentOutlet="OtherModuleComponent;
  9. ngModuleFactory: myModule;"></ng-container>`
  10. })
  11. class NgTemplateOutletOtherModuleExample {
  12. // This field is necessary to expose OtherModuleComponent to the template.
  13. OtherModuleComponent = OtherModuleComponent;
  14. myModule: NgModuleFactory<any>;
  15.  
  16. constructor(compiler: Compiler) { this.myModule = compiler.compileModuleSync(OtherModule); }
  17. }

方法

ngOnChanges(changes: SimpleChanges)
      
      ngOnChanges(changes: SimpleChanges)
    
参数
changes SimpleChanges
ngOnDestroy()
      
      ngOnDestroy()
    
参数

没有参数。