1
votes

I have an ng1 component in javascript written as requirejs module. I need to use this component inside my ng2 component (until my old ng1 app is fully ported to ng2). Here is the plnkr : https://plnkr.co/edit/IYuoZZE4sPGr97pxB04l?p=preview Here is my bootstrap logic:

import {App} from './app';
import {upgradeAdapter} from './adapter';

var appComponent = upgradeAdapter.downgradeNg2Component(App);

requirejs( ['ng1App'], 
function(ng1App) {
    angular.module('ng1App').directive('myApp',appComponent); 
    // following line throws error
    upgradeAdapter.bootstrap(document.body, ['ng1App']);    
} );

it gives me following error

angular2-polyfills.js:286 Uncaught Error: AngularJS v1.x is not loaded!

Appreciate any pointers.

The other question i have is how do i import the requirejs modules for individual ng1 components in my ng2 component :

import {Component} from '@angular/core'
import {upgradeAdapter} from './adapter';

// How do i dynamically load the ng1-comp rquirejs module here?

@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
    <ng1-comp></ng1-comp>      
  `,
  directives: [upgradeAdapter.upgradeNg1Component('ng1Comp')]
})
export class App {
  constructor() {
    this.name = 'Angular2 (Release Candidate!)'
  }
}
1
Why not upgrade your ng1 component to angular 2? It should be easy, app.module('mymodule').component('mycomp', {controller:{} }...null canvas

1 Answers

0
votes

Finally figured it out.. The requirejs config needs to be converted to systemjs or other module loader config. Converting to system js is easy..

// map systemJS apis to corresponding apis in requirejs
window.require = System.amdRequire;
window.requirejs = System.amdRequire;
window.define = System.define;
System.config(function getNG1Config() {
  return {
    defaultJSExtensions: true,
    baseUrl: '/',
    waitSeconds: 0,
    paths: {
      'angular-ui-router': 'https://npmcdn.com/[email protected]/release/', //needs to be defined before 'angular'
      'angular': 'https://npmcdn.com/[email protected]/', 
      '@angular/common': 'https://npmcdn.com/@angular/[email protected]/bundles/common.umd',
      '@angular/compiler': 'https://npmcdn.com/@angular/[email protected]/bundles/compiler.umd',
      '@angular/core': 'https://npmcdn.com/@angular/[email protected]/bundles/core.umd',
      '@angular/http': 'https://npmcdn.com/@angular/[email protected]/bundles/http.umd',
      '@angular/platform-browser': 'https://npmcdn.com/@angular/[email protected]/bundles/platform-browser.umd',
      '@angular/platform-browser-dynamic': 'https://npmcdn.com/@angular/[email protected]/bundles/platform-browser-dynamic.umd',
      '@angular/router-deprecated': 'https://npmcdn.com/@angular/[email protected]/bundles/router-deprecated.umd',
      '@angular/upgrade': 'https://npmcdn.com/@angular/[email protected]/bundles/upgrade.umd',
      'upgradeAdapter': './upgradeAdapter',
      'rxjs/*': 'https://npmcdn.com/[email protected]/bundles/Rx.umd',

      'app': 'src/app',
      'bootstrap': './bootstrap'
    },
    map: {
    },
    meta: {
      'app': {
        deps: ['angular', 'angular-ui-router']
      },
      'bootstrap': {
        deps: ['angular', 'angular-ui-router']
      }
    },
    packages: {
      'angular': {
        main: 'angular.min.js',
        defaultExtension: 'js'
      },
      'angular-ui-router': {
        main: 'angular-ui-router.js',
        defaultExtension: 'js'
      },
      'rxjs': {
        defaultExtension: 'js'
      },
      'angular2-in-memory-web-api': {
        defaultExtension: 'js'
      },
      './': {
        defaultExtension: 'js'
      },
      'upgradeAdapter': {
        defaultExtension: 'js'
      }
    }
  };
});

Here is the plunker : https://plnkr.co/edit/SnBtwp?p=preview