{"id":13990,"date":"2017-03-17T09:00:46","date_gmt":"2017-03-17T00:00:46","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=13990"},"modified":"2018-11-14T16:33:43","modified_gmt":"2018-11-14T07:33:43","slug":"creating-a-hybrid-environment-in-angular-using-upgrademodule","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2017\/03\/17\/creating-a-hybrid-environment-in-angular-using-upgrademodule\/","title":{"rendered":"Creating a hybrid environment in Angular using UpgradeModule"},"content":{"rendered":"
<\/p>\n
<\/p>\n
Google released the first stable version of Angular by the end of 2016 and since Angular has been completely rewritten and is not an upgrade of AngularJS, it is a big break change for companies that already have quite robust system built in AngularJS in their frontend.<\/p>\n
Having said that, I decided to write and explain step by step how to utilize UpgradeModule library from Angular and have an AngularJS application that allows Angular components within in its application, and not only that but also by utilizing it, it is also possible achieve communication between AngularJS controllers and Angular services and vice versa.<\/p>\n
For demonstration purposes, I will be using Plunker<\/a>. I will also explain step by step how to set the correct hybrid environment in Plunker as well.<\/p>\n From here<\/a>\u00a0click on the new dropdown button and select \u201cAngular 1.5\u201d, exclude the \u201cAngular 1.5 + Typescript\u201d option because in future articles I want to show how AngularJS controllers, directives, services completely written in Javascript communicate with Angular components.<\/p>\n Then, from the menu on the left, create a new file named \"config.js\". That file will utilize a module loader, which will be mapping and telling Angular where to look when a library is being invoked.<\/p>\n This file will be utilizing SystemJS<\/a>, but there are some other options such as Webpack <\/a><\/span>\u00a0and Browserify<\/a>.<\/span> config.js<\/p>\n Then we have to prepare the index.html so that it loads the config.js we just created and some core libraries needed (zonejs, reflectjs, systemjs) by Angular. \u00a0Also we need to delete the AngularJS code created by plunker so that it does not bootstrap.<\/p>\n Later on after editing the AngularJS controller created by Plunker(app.js) and creating a new Angular component, we will return to this file to call both components (AngularJS and Angular) from here.<\/p>\n After editing index.html, it should look something similar as below.<\/p>\n index.html<\/p>\n<\/a><\/p>\n
Mapping core Angular libraries<\/h2>\n
\nAfter creating the file, we just can copy and paste the following code that includes the libraries we will be utilizing this time.<\/p>\nSystem.config({\r\n \/\/use typescript for compilation\r\n transpiler: 'typescript',\r\n \/\/typescript compiler options\r\n typescriptOptions: {\r\n emitDecoratorMetadata: true\r\n },\r\n paths: {\r\n 'npm:': 'https:\/\/unpkg.com\/'\r\n },\r\n \/\/map tells the System loader where to look for things\r\n map: {\r\n\r\n 'app': '.\/src',\r\n '@angular\/core': 'npm:@angular\/core\/bundles\/core.umd.js',\r\n '@angular\/common': 'npm:@angular\/common\/bundles\/common.umd.js',\r\n '@angular\/compiler': 'npm:@angular\/compiler\/bundles\/compiler.umd.js',\r\n '@angular\/platform-browser': 'npm:@angular\/platform-browser\/bundles\/platform-browser.umd.js',\r\n '@angular\/platform-browser-dynamic': 'npm:@angular\/platform-browser-dynamic\/bundles\/platform-browser-dynamic.umd.js',\r\n '@angular\/http': 'npm:@angular\/http\/bundles\/http.umd.js',\r\n '@angular\/router': 'npm:@angular\/router\/bundles\/router.umd.js',\r\n '@angular\/forms': 'npm:@angular\/forms\/bundles\/forms.umd.js',\r\n\r\n '@angular\/core\/testing': 'npm:@angular\/core\/bundles\/core-testing.umd.js',\r\n '@angular\/common\/testing': 'npm:@angular\/common\/bundles\/common-testing.umd.js',\r\n '@angular\/compiler\/testing': 'npm:@angular\/compiler\/bundles\/compiler-testing.umd.js',\r\n '@angular\/platform-browser\/testing': 'npm:@angular\/platform-browser\/bundles\/platform-browser-testing.umd.js',\r\n '@angular\/platform-browser-dynamic\/testing': 'npm:@angular\/platform-browser-dynamic\/bundles\/platform-browser-dynamic-testing.umd.js',\r\n '@angular\/http\/testing': 'npm:@angular\/http\/bundles\/http-testing.umd.js',\r\n '@angular\/router\/testing': 'npm:@angular\/router\/bundles\/router-testing.umd.js',\r\n '@angular\/upgrade': 'npm:@angular\/upgrade\/bundles\/upgrade.umd.js',\r\n '@angular\/upgrade\/static': 'npm:@angular\/upgrade\/bundles\/upgrade-static.umd.js',\r\n 'rxjs': 'npm:rxjs',\r\n 'typescript': 'npm:typescript@2.0.2\/lib\/typescript.js'\r\n },\r\n \/\/packages defines our app package\r\n packages: {\r\n \/\/this file will be used for bootstrapping our application.\r\n app: {\r\n main: '.\/main.ts',\r\n defaultExtension: 'ts'\r\n },\r\n app_component_ts: {\r\n \/\/ this will our future file where we will create our Angular component.\r\n main: '.\/my-university.ts',\r\n defaultExtension: 'ts'\r\n },\r\n rxjs: {\r\n defaultExtension: 'js'\r\n }\r\n }\r\n});\r\n<\/pre>\n
Adding core libraries to index.html<\/h2>\n
\r\n \r\n\r\n \r\n \r\n