|
| 1 | +import * as angular from 'angular'; |
| 2 | +import { Component, NgModule } from '@angular/core'; |
| 3 | +import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 4 | +import { UpgradeModule } from '@angular/upgrade/static'; |
| 5 | +import { BrowserModule } from '@angular/platform-browser'; |
| 6 | +import { UIRouterModule } from '@uirouter/angular'; |
| 7 | +import { UIRouterUpgradeModule } from '@uirouter/angular-hybrid'; |
| 8 | +import { UrlService} from '@uirouter/core'; |
| 9 | + |
| 10 | + |
| 11 | +var app = angular.module('minimal', ['ui.router.upgrade']); |
| 12 | + |
| 13 | +app.run(($stateRegistry, $urlService) => { |
| 14 | + $urlService.rules.initial({state: 'app'}); |
| 15 | + |
| 16 | + $stateRegistry.register({ |
| 17 | + url: '', |
| 18 | + name: 'app', |
| 19 | + template: ` |
| 20 | + <a ui-sref=".ng1" ui-sref-active-eq="active">app.ng1</a> |
| 21 | + <a ui-sref=".ng1.ng2" ui-sref-active-eq="active">app.ng1.ng2</a> |
| 22 | + <a ui-sref=".ng2" ui-sref-active-eq="active">app.ng2</a> |
| 23 | + <a ui-sref=".ng2.ng2" ui-sref-active-eq="active">app.ng2.ng2</a> |
| 24 | + <ui-view></ui-view> |
| 25 | + ` |
| 26 | + }); |
| 27 | + |
| 28 | + // route to ng1 component |
| 29 | + $stateRegistry.register({ |
| 30 | + url: '/ng1', |
| 31 | + name: 'app.ng1', |
| 32 | + component: 'ng1Component', |
| 33 | + }); |
| 34 | + |
| 35 | + // nested route to ng2 component |
| 36 | + $stateRegistry.register({ |
| 37 | + url: '/ng2', |
| 38 | + name: 'app.ng1.ng2', |
| 39 | + component: Ng2Component, |
| 40 | + }); |
| 41 | + |
| 42 | + // route to ng2 component |
| 43 | + $stateRegistry.register({ |
| 44 | + url: '/ng2', |
| 45 | + name: 'app.ng2', |
| 46 | + component: Ng2Component, |
| 47 | + }); |
| 48 | + |
| 49 | + // nested route to ng2 component |
| 50 | + $stateRegistry.register({ |
| 51 | + url: '/ng2', |
| 52 | + name: 'app.ng2.ng2', |
| 53 | + component: Ng2Component, |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +// An AngularJS component |
| 60 | +app.component('ng1Component', { |
| 61 | + template: ` |
| 62 | + <h1>ng1 component</h1> |
| 63 | + <a ui-sref="app">Back to app</a> |
| 64 | + <ui-view></ui-view> |
| 65 | + ` |
| 66 | +}) |
| 67 | + |
| 68 | +// An Angular component |
| 69 | +@Component({ |
| 70 | + selector: 'ng2-component', |
| 71 | + template: ` |
| 72 | + <h1>ng2 component</h1> |
| 73 | + <a uiSref="app">Back to app</a> |
| 74 | + <ui-view></ui-view> |
| 75 | + ` |
| 76 | +}) export class Ng2Component { } |
| 77 | + |
| 78 | +// The root Angular module |
| 79 | +@NgModule({ |
| 80 | + imports: [ |
| 81 | + BrowserModule, |
| 82 | + // Provide Angular upgrade capabilities |
| 83 | + UpgradeModule, |
| 84 | + // Provides the @uirouter/angular-hybrid directives |
| 85 | + UIRouterUpgradeModule, |
| 86 | + // Provides the @uirouter/angular directives |
| 87 | + UIRouterModule, |
| 88 | + ], |
| 89 | + declarations: [Ng2Component], |
| 90 | + entryComponents: [Ng2Component], |
| 91 | +}) export class RootModule { |
| 92 | + ngDoBootstrap() { |
| 93 | + /* no body: this disables normal (non-hybrid) Angular bootstrapping */ |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Using AngularJS config block, call `deferIntercept()`. |
| 98 | +// This tells UI-Router to delay the initial URL sync (until all bootstrapping is complete) |
| 99 | +app.config([ '$urlServiceProvider', $urlService => $urlService.deferIntercept() ]); |
| 100 | + |
| 101 | +// Manually bootstrap the Angular app |
| 102 | +platformBrowserDynamic().bootstrapModule(RootModule).then(platformRef => { |
| 103 | + const injector = platformRef.injector; |
| 104 | + const upgrade = injector.get(UpgradeModule) as UpgradeModule; |
| 105 | + |
| 106 | + // The DOM must be already be available |
| 107 | + upgrade.bootstrap(document.body, [app.name]); |
| 108 | + |
| 109 | + // Intialize the Angular Module (get() any UIRouter service from DI to initialize it) |
| 110 | + const url = injector.get(UrlService); |
| 111 | + |
| 112 | + // Instruct UIRouter to listen to URL changes |
| 113 | + url.listen(); |
| 114 | + url.sync(); |
| 115 | +}); |
0 commit comments