improve angular1 type declaration in hybrid apps
This commit is contained in:
parent
8e27ff9162
commit
4ac5e60522
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
// #docregion ngmodule
|
// #docregion ngmodule
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
declare var angular: any;
|
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.controller('MainCtrl', function() {
|
.controller('MainCtrl', function() {
|
||||||
this.message = 'Hello world';
|
this.message = 'Hello world';
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
declare var angular: any;
|
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.controller('MainCtrl', function() {
|
.controller('MainCtrl', function() {
|
||||||
this.message = 'Hello world';
|
this.message = 'Hello world';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -28,7 +28,7 @@ angular.module('heroApp', [])
|
||||||
.directive('heroDetail', downgradeComponent({
|
.directive('heroDetail', downgradeComponent({
|
||||||
component: HeroDetailComponent,
|
component: HeroDetailComponent,
|
||||||
inputs: ['hero']
|
inputs: ['hero']
|
||||||
}));
|
}) as angular.IDirectiveFactory);
|
||||||
|
|
||||||
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
||||||
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -34,7 +34,10 @@ export class AppModule {
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.service('heroes', HeroesService)
|
.service('heroes', HeroesService)
|
||||||
.directive('heroDetail', downgradeComponent({component: HeroDetailComponent}));
|
.directive(
|
||||||
|
'heroDetail',
|
||||||
|
downgradeComponent({component: HeroDetailComponent}) as angular.IDirectiveFactory
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -28,7 +28,10 @@ export class AppModule {
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.component('heroDetail', heroDetail)
|
.component('heroDetail', heroDetail)
|
||||||
.directive('myContainer', downgradeComponent({component: ContainerComponent}));
|
.directive(
|
||||||
|
'myContainer',
|
||||||
|
downgradeComponent({component: ContainerComponent}) as angular.IDirectiveFactory
|
||||||
|
);
|
||||||
|
|
||||||
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
||||||
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -33,7 +33,7 @@ angular.module('heroApp', [])
|
||||||
component: HeroDetailComponent,
|
component: HeroDetailComponent,
|
||||||
inputs: ['hero'],
|
inputs: ['hero'],
|
||||||
outputs: ['deleted']
|
outputs: ['deleted']
|
||||||
}));
|
}) as angular.IDirectiveFactory);
|
||||||
|
|
||||||
// #enddocregion downgradecomponent
|
// #enddocregion downgradecomponent
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -29,7 +29,10 @@ export class AppModule {
|
||||||
import { downgradeComponent } from '@angular/upgrade/static';
|
import { downgradeComponent } from '@angular/upgrade/static';
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.directive('heroDetail', downgradeComponent({component: HeroDetailComponent}));
|
.directive(
|
||||||
|
'heroDetail',
|
||||||
|
downgradeComponent({component: HeroDetailComponent}) as angular.IDirectiveFactory
|
||||||
|
);
|
||||||
|
|
||||||
// #enddocregion downgradecomponent
|
// #enddocregion downgradecomponent
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -28,7 +28,10 @@ export class AppModule {
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.component('heroDetail', heroDetail)
|
.component('heroDetail', heroDetail)
|
||||||
.directive('myContainer', downgradeComponent({component: ContainerComponent}));
|
.directive(
|
||||||
|
'myContainer',
|
||||||
|
downgradeComponent({component: ContainerComponent}) as angular.IDirectiveFactory
|
||||||
|
);
|
||||||
|
|
||||||
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
||||||
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { BrowserModule } from '@angular/platform-browser';
|
import { BrowserModule } from '@angular/platform-browser';
|
||||||
|
@ -30,7 +30,10 @@ export class AppModule {
|
||||||
|
|
||||||
angular.module('heroApp', [])
|
angular.module('heroApp', [])
|
||||||
.component('heroDetail', heroDetail)
|
.component('heroDetail', heroDetail)
|
||||||
.directive('myContainer', downgradeComponent({component: ContainerComponent}));
|
.directive(
|
||||||
|
'myContainer',
|
||||||
|
downgradeComponent({component: ContainerComponent}) as angular.IDirectiveFactory
|
||||||
|
);
|
||||||
|
|
||||||
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
platformBrowserDynamic().bootstrapModule(AppModule).then(platformRef => {
|
||||||
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
let upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// #docregion import-adapter
|
// #docregion import-adapter
|
||||||
declare var angular: any;
|
declare var angular: angular.IAngularStatic;
|
||||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||||
import { UpgradeModule, downgradeComponent, downgradeInjectable } from '@angular/upgrade/static';
|
import { UpgradeModule, downgradeComponent, downgradeInjectable } from '@angular/upgrade/static';
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// #docregion
|
// #docregion
|
||||||
|
declare var angular: angular.IAngularStatic;
|
||||||
import { Phone, PhoneData } from '../core/phone/phone.service';
|
import { Phone, PhoneData } from '../core/phone/phone.service';
|
||||||
declare var angular: any;
|
|
||||||
|
|
||||||
class PhoneDetailController {
|
class PhoneDetailController {
|
||||||
phone: PhoneData;
|
phone: PhoneData;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// #docregion
|
// #docregion
|
||||||
|
declare var angular: angular.IAngularStatic;
|
||||||
import { Phone, PhoneData } from '../core/phone/phone.service';
|
import { Phone, PhoneData } from '../core/phone/phone.service';
|
||||||
declare var angular: any;
|
|
||||||
|
|
||||||
class PhoneListController {
|
class PhoneListController {
|
||||||
phones: PhoneData[];
|
phones: PhoneData[];
|
||||||
|
|
|
@ -1095,15 +1095,23 @@ code-example(format="").
|
||||||
|
|
||||||
.l-sub-section
|
.l-sub-section
|
||||||
:marked
|
:marked
|
||||||
### Why declare _angular_ as _any_?
|
### Why declare _angular_ as _angular.IAngularStatic_?
|
||||||
|
|
||||||
A strongly typed `angular` reference to Angular 1 would be great.
|
`@types/angular` is declared as a UMD module, and due to the way
|
||||||
But we can't import its <a href="https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-umd-module-definitions" target="_blank">UMD typings</a>
|
<a href="https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#support-for-umd-module-definitions" target="_blank">UMD typings</a>
|
||||||
library, `@types/angular`, without also importing Angular 1 itself via `import * as angular from 'angular'`.
|
work, once you have an ES6 `import` statement in a file all UMD typed modules must also be
|
||||||
|
imported via `import` statements instead of being globally available.
|
||||||
|
|
||||||
Angular 1 is currently loaded by a script tag in `index.html` and
|
Angular 1 is currently loaded by a script tag in `index.html`, which means that the whole app
|
||||||
switching to an ES6 import at this time is not worth the considerable effort.
|
has access to it as a global and uses the same instance of the `angular` variable.
|
||||||
Instead we declare `angular` as an untyped `any` to avoid typing errors.
|
If we used `import * as angular from 'angular'` instead we would also need to overhaul how we
|
||||||
|
load every file in our Angular 1 app to use ES6 modules in order to ensure Angular 1 was being
|
||||||
|
loaded correctly.
|
||||||
|
|
||||||
|
This is a considerable effort and it often isn't worth it, especially since we are in the
|
||||||
|
process of moving our our to Angular 2 already.
|
||||||
|
Instead we declare `angular` as `angular.IAngularStatic` to indicate it is a global variable
|
||||||
|
and still have full typing support.
|
||||||
|
|
||||||
:marked
|
:marked
|
||||||
We can then make an adapter by instantiating the class:
|
We can then make an adapter by instantiating the class:
|
||||||
|
|
Loading…
Reference in New Issue