2016-11-16 05:33:31 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
2017-08-17 08:56:04 -04:00
|
|
|
// #docplaster
|
2020-02-02 20:49:28 -05:00
|
|
|
import {Component, Directive, ElementRef, EventEmitter, Injectable, Injector, Input, NgModule, Output} from '@angular/core';
|
2016-11-16 05:33:31 -05:00
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
|
|
|
import {UpgradeComponent, UpgradeModule, downgradeComponent, downgradeInjectable} from '@angular/upgrade/static';
|
|
|
|
|
2017-08-02 10:03:17 -04:00
|
|
|
declare var angular: ng.IAngularStatic;
|
|
|
|
|
2019-03-22 05:42:52 -04:00
|
|
|
export interface Hero {
|
2016-11-16 05:33:31 -05:00
|
|
|
name: string;
|
|
|
|
description: string;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:56:04 -04:00
|
|
|
// #docregion ng1-text-formatter-service
|
2019-03-22 05:42:52 -04:00
|
|
|
export class TextFormatter {
|
2017-08-17 08:56:04 -04:00
|
|
|
titleCase(value: string) { return value.replace(/((^|\s)[a-z])/g, (_, c) => c.toUpperCase()); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// #enddocregion
|
2017-01-27 01:30:42 -05:00
|
|
|
// #docregion Angular Stuff
|
2016-11-16 05:33:31 -05:00
|
|
|
// #docregion ng2-heroes
|
2017-01-27 01:30:42 -05:00
|
|
|
// This Angular component will be "downgraded" to be used in AngularJS
|
2016-11-16 05:33:31 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'ng2-heroes',
|
|
|
|
// This template uses the upgraded `ng1-hero` component
|
2017-01-27 01:30:42 -05:00
|
|
|
// Note that because its element is compiled by Angular we must use camelCased attribute names
|
2017-03-13 20:34:53 -04:00
|
|
|
template: `<header><ng-content selector="h1"></ng-content></header>
|
|
|
|
<ng-content selector=".extra"></ng-content>
|
2016-11-16 05:33:31 -05:00
|
|
|
<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>`,
|
|
|
|
})
|
2019-03-22 05:42:52 -04:00
|
|
|
export class Ng2HeroesComponent {
|
2018-06-18 19:38:33 -04:00
|
|
|
@Input() heroes !: Hero[];
|
2016-11-16 05:33:31 -05:00
|
|
|
@Output() addHero = new EventEmitter();
|
|
|
|
@Output() removeHero = new EventEmitter();
|
|
|
|
}
|
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion ng2-heroes-service
|
2017-01-27 01:30:42 -05:00
|
|
|
// This Angular service will be "downgraded" to be used in AngularJS
|
2016-11-16 05:33:31 -05:00
|
|
|
@Injectable()
|
2019-03-22 05:42:52 -04:00
|
|
|
export class HeroesService {
|
2016-11-16 05:33:31 -05:00
|
|
|
heroes: Hero[] = [
|
|
|
|
{name: 'superman', description: 'The man of steel'},
|
|
|
|
{name: 'wonder woman', description: 'Princess of the Amazons'},
|
|
|
|
{name: 'thor', description: 'The hammer-wielding god'}
|
|
|
|
];
|
|
|
|
|
|
|
|
// #docregion use-ng1-upgraded-service
|
2017-08-17 08:56:04 -04:00
|
|
|
constructor(textFormatter: TextFormatter) {
|
2017-01-27 01:30:42 -05:00
|
|
|
// Change all the hero names to title case, using the "upgraded" AngularJS service
|
2017-08-17 08:56:04 -04:00
|
|
|
this.heroes.forEach((hero: Hero) => hero.name = textFormatter.titleCase(hero.name));
|
2016-11-16 05:33:31 -05:00
|
|
|
}
|
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
addHero() {
|
|
|
|
this.heroes =
|
|
|
|
this.heroes.concat([{name: 'Kamala Khan', description: 'Epic shape-shifting healer'}]);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeHero(hero: Hero) { this.heroes = this.heroes.filter((item: Hero) => item !== hero); }
|
|
|
|
}
|
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion ng1-hero-wrapper
|
2017-01-27 01:30:42 -05:00
|
|
|
// This Angular directive will act as an interface to the "upgraded" AngularJS component
|
2016-11-16 05:33:31 -05:00
|
|
|
@Directive({selector: 'ng1-hero'})
|
2019-03-22 05:42:52 -04:00
|
|
|
export class Ng1HeroComponentWrapper extends UpgradeComponent {
|
2016-11-16 05:33:31 -05:00
|
|
|
// The names of the input and output properties here must match the names of the
|
2017-01-27 01:30:42 -05:00
|
|
|
// `<` and `&` bindings in the AngularJS component that is being wrapped
|
2018-06-18 19:38:33 -04:00
|
|
|
@Input() hero !: Hero;
|
|
|
|
@Output() onRemove !: EventEmitter<void>;
|
2017-08-02 12:27:22 -04:00
|
|
|
|
|
|
|
constructor(elementRef: ElementRef, injector: Injector) {
|
2017-01-27 01:30:42 -05:00
|
|
|
// We must pass the name of the directive as used by AngularJS to the super
|
2016-11-16 05:33:31 -05:00
|
|
|
super('ng1Hero', elementRef, injector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion ng2-module
|
2017-01-27 01:30:42 -05:00
|
|
|
// This NgModule represents the Angular pieces of the application
|
2016-11-16 05:33:31 -05:00
|
|
|
@NgModule({
|
|
|
|
declarations: [Ng2HeroesComponent, Ng1HeroComponentWrapper],
|
|
|
|
providers: [
|
|
|
|
HeroesService,
|
|
|
|
// #docregion upgrade-ng1-service
|
2017-01-27 01:30:42 -05:00
|
|
|
// Register an Angular provider whose value is the "upgraded" AngularJS service
|
2017-08-17 08:56:04 -04:00
|
|
|
{provide: TextFormatter, useFactory: (i: any) => i.get('textFormatter'), deps: ['$injector']}
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
],
|
|
|
|
// All components that are to be "downgraded" must be declared as `entryComponents`
|
|
|
|
entryComponents: [Ng2HeroesComponent],
|
2017-01-27 01:30:42 -05:00
|
|
|
// We must import `UpgradeModule` to get access to the AngularJS core services
|
2016-11-16 05:33:31 -05:00
|
|
|
imports: [BrowserModule, UpgradeModule]
|
|
|
|
})
|
2017-08-02 12:27:22 -04:00
|
|
|
// #docregion bootstrap-ng1
|
2019-03-22 05:42:52 -04:00
|
|
|
export class Ng2AppModule {
|
2017-08-02 12:27:22 -04:00
|
|
|
// #enddocregion ng2-module
|
|
|
|
constructor(private upgrade: UpgradeModule) {}
|
|
|
|
|
|
|
|
ngDoBootstrap() {
|
|
|
|
// We bootstrap the AngularJS app.
|
|
|
|
this.upgrade.bootstrap(document.body, [ng1AppModule.name]);
|
2016-11-16 05:33:31 -05:00
|
|
|
}
|
2017-08-02 12:27:22 -04:00
|
|
|
// #docregion ng2-module
|
2016-11-16 05:33:31 -05:00
|
|
|
}
|
2017-08-02 12:27:22 -04:00
|
|
|
// #enddocregion bootstrap-ng1
|
|
|
|
// #enddocregion ng2-module
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
|
|
|
|
// #docregion Angular 1 Stuff
|
|
|
|
// #docregion ng1-module
|
2017-01-27 01:30:42 -05:00
|
|
|
// This Angular 1 module represents the AngularJS pieces of the application
|
2019-03-22 05:42:52 -04:00
|
|
|
export const ng1AppModule: ng.IModule = angular.module('ng1AppModule', []);
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion ng1-hero
|
2017-01-27 01:30:42 -05:00
|
|
|
// This AngularJS component will be "upgraded" to be used in Angular
|
2016-11-16 05:33:31 -05:00
|
|
|
ng1AppModule.component('ng1Hero', {
|
|
|
|
bindings: {hero: '<', onRemove: '&'},
|
|
|
|
transclude: true,
|
|
|
|
template: `<div class="title" ng-transclude></div>
|
|
|
|
<h2>{{ $ctrl.hero.name }}</h2>
|
|
|
|
<p>{{ $ctrl.hero.description }}</p>
|
|
|
|
<button ng-click="$ctrl.onRemove()">Remove</button>`
|
|
|
|
});
|
|
|
|
// #enddocregion
|
|
|
|
|
2017-08-17 08:56:04 -04:00
|
|
|
// #docregion ng1-text-formatter-service
|
2017-01-27 01:30:42 -05:00
|
|
|
// This AngularJS service will be "upgraded" to be used in Angular
|
2017-08-17 08:56:04 -04:00
|
|
|
ng1AppModule.service('textFormatter', [TextFormatter]);
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion downgrade-ng2-heroes-service
|
2017-01-27 01:30:42 -05:00
|
|
|
// Register an AngularJS service, whose value is the "downgraded" Angular injectable.
|
2017-09-08 21:40:32 -04:00
|
|
|
ng1AppModule.factory('heroesService', downgradeInjectable(HeroesService) as any);
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion ng2-heroes-wrapper
|
2017-08-02 10:03:17 -04:00
|
|
|
// This directive will act as the interface to the "downgraded" Angular component
|
2017-03-14 17:55:37 -04:00
|
|
|
ng1AppModule.directive('ng2Heroes', downgradeComponent({component: Ng2HeroesComponent}));
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
// #docregion example-app
|
|
|
|
// This is our top level application component
|
|
|
|
ng1AppModule.component('exampleApp', {
|
2017-01-27 01:30:42 -05:00
|
|
|
// We inject the "downgraded" HeroesService into this AngularJS component
|
|
|
|
// (We don't need the `HeroesService` type for AngularJS DI - it just helps with TypeScript
|
2016-11-16 05:33:31 -05:00
|
|
|
// compilation)
|
2017-08-02 10:03:17 -04:00
|
|
|
controller: [
|
|
|
|
'heroesService', function(heroesService: HeroesService) { this.heroesService = heroesService; }
|
|
|
|
],
|
|
|
|
// This template makes use of the downgraded `ng2-heroes` component
|
|
|
|
// Note that because its element is compiled by AngularJS we must use kebab-case attributes
|
|
|
|
// for inputs and outputs
|
|
|
|
template: `<link rel="stylesheet" href="./styles.css">
|
|
|
|
<ng2-heroes [heroes]="$ctrl.heroesService.heroes" (add-hero)="$ctrl.heroesService.addHero()" (remove-hero)="$ctrl.heroesService.removeHero($event)">
|
|
|
|
<h1>Heroes</h1>
|
|
|
|
<p class="extra">There are {{ $ctrl.heroesService.heroes.length }} heroes.</p>
|
|
|
|
</ng2-heroes>`
|
|
|
|
});
|
2017-08-02 12:27:22 -04:00
|
|
|
// #enddocregion
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|
|
|
|
|
|
|
|
|
2017-08-02 12:27:22 -04:00
|
|
|
// #docregion bootstrap-ng2
|
|
|
|
// We bootstrap the Angular module as we would do in a normal Angular app.
|
2020-02-02 20:49:28 -05:00
|
|
|
// (We are using the dynamic browser platform as this example has not been compiled AOT.)
|
2017-08-02 12:27:22 -04:00
|
|
|
platformBrowserDynamic().bootstrapModule(Ng2AppModule);
|
2016-11-16 05:33:31 -05:00
|
|
|
// #enddocregion
|