填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

downgradeComponent

A helper function that allows an Angular component to be used from AngularJS.

查看"说明"...

downgradeComponent(info: { component: Type<any>; downgradedModule?: string; propagateDigest?: boolean; inputs?: string[]; outputs?: string[]; selectors?: string[]; }): any
      
      downgradeComponent(info: { component: Type<any>; downgradedModule?: string; propagateDigest?: boolean; inputs?: string[]; outputs?: string[]; selectors?: string[]; }): any
    
参数
info object

contains information about the Component that is being downgraded:

  • component: Type<any>: The type of the Component that will be downgraded
  • downgradedModule?: string: The name of the downgraded module (if any) that the component "belongs to", as returned by a call to downgradeModule(). It is the module, whose corresponding Angular module will be bootstrapped, when the component needs to be instantiated.
    (This option is only necessary when using downgradeModule() to downgrade more than one Angular module.)
  • propagateDigest?: boolean: Whether to perform change detection on the component on every $digest. If set to false, change detection will still be performed when any of the component's inputs changes. (Default: true)
返回值

any: a factory function that can be used to register the component in an AngularJS module.

说明

Part of the upgrade/static library for hybrid upgrade apps that support AOT compilation

This helper function returns a factory function to be used for registering an AngularJS wrapper directive for "downgrading" an Angular component.

使用说明

Examples

Let's assume that you have an Angular component called ng2Heroes that needs to be made available in AngularJS templates.

// This Angular component will be "downgraded" to be used in AngularJS @Component({ selector: 'ng2-heroes', // This template uses the upgraded `ng1-hero` component // Note that because its element is compiled by Angular we must use camelCased attribute names template: `<header><ng-content selector="h1"></ng-content></header> <ng-content selector=".extra"></ng-content> <div *ngFor="let hero of heroes"> <ng1-hero [hero]="hero" (onRemove)="removeHero.emit(hero)"><strong>Super Hero</strong></ng1-hero> </div> <button (click)="addHero.emit()">Add Hero</button>`, }) export class Ng2HeroesComponent { @Input() heroes !: Hero[]; @Output() addHero = new EventEmitter(); @Output() removeHero = new EventEmitter(); }
      
      // This Angular component will be "downgraded" to be used in AngularJS
@Component({
  selector: 'ng2-heroes',
  // This template uses the upgraded `ng1-hero` component
  // Note that because its element is compiled by Angular we must use camelCased attribute names
  template: `<header><ng-content selector="h1"></ng-content></header>
             <ng-content selector=".extra"></ng-content>
             <div *ngFor="let hero of heroes">
               <ng1-hero [hero]="hero" (onRemove)="removeHero.emit(hero)"><strong>Super Hero</strong></ng1-hero>
             </div>
             <button (click)="addHero.emit()">Add Hero</button>`,
})
export class Ng2HeroesComponent {
  @Input() heroes !: Hero[];
  @Output() addHero = new EventEmitter();
  @Output() removeHero = new EventEmitter();
}
    

We must create an AngularJS directive that will make this Angular component available inside AngularJS templates. The downgradeComponent() function returns a factory function that we can use to define the AngularJS directive that wraps the "downgraded" component.

// This directive will act as the interface to the "downgraded" Angular component ng1AppModule.directive('ng2Heroes', downgradeComponent({component: Ng2HeroesComponent}));
      
      // This directive will act as the interface to the "downgraded" Angular component
ng1AppModule.directive('ng2Heroes', downgradeComponent({component: Ng2HeroesComponent}));
    

For more details and examples on downgrading Angular components to AngularJS components please visit the Upgrade guide.