


Angular1.x and angular2+ run in parallel, angular1.x upgrade angular2+ solution
This article introduces the solution for upgrading angularjs2 from angularjs1.x, and also introduces the parallelism between angularjs1.x and angularjs2. Now let’s take a look at this article together
angular1.x upgrade angular2 plan
I provide you with a parallel and incremental upgrade plan for angular1.x and angular5, so that you can upgrade step by step For your own application, if you don’t want to read the text, just start the demo migration-from-angular1.x-to-angular2Plus
Option 1: The main body is angular1.x, and gradually add service, Component, filter, controller, route, and dependencies are upgraded to angular5
Option 2: The main body is angular5. All js files in the project are processed once, and each js file is processed using ES6. module
export, and then gradually move the content closer to angular5
I recommend choosing option 1 for incremental upgrade, by running these two frameworks together in the same application, and Migrate AngularJS components to Angular one by one. You can upgrade the application without interrupting other businesses, because this work can be completed by multiple people and gradually rolled out over a period of time. The following is an explanation of Option 1
Hybrid APP Mainly relies on Angular to provide upgrade/static modules. You will see it everywhere in the future. The following will teach you step by step how to migrate angular1. to the angular.module property. In Angular, we create one or more classes with NgModule decorators, which are used to describe Angular resources in metadata. In a hybrid application, we run two versions of Angular simultaneously. This means we need at least one module each from AngularJS and Angular. To bootstrap a hybrid application, we have to bootstrap both Angular and AngularJS in the application. You need to bootstrap Angular first, and then call UpgradeModule to bootstrap AngularJS.
Remove the ng-app and ng-strict-di directives from the HTML, create an app.module.ts file, and add the following NgModule class:import { UpgradeModule } from '@angular/upgrade/static'; @NgModule({ imports: [ UpgradeModule ] }) export class AppModule { constructor(private upgrade: UpgradeModule) { } ngDoBootstrap() { this.upgrade.bootstrap(document.body, ['yourAngularJsAppName'], { strictDi: true }); } }
import {AppModule} from './app/app.module'; import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"; platformBrowserDynamic().bootstrapModule(AppModule) .catch(err => console.log(err));
2. Gradually upgrade the services in the project to angular5
We upgrade the content in username-service.js to username-service.ts:import { Injectable } from '@angular/core'; @Injectable() export class UsernameService { get() { return 'nina' } }
downgrade-services.ts:
import * as angular from 'angular'; import { downgradeInjectable } from '@angular/upgrade/static'; import { UsernameService } from './services/ username-service '; angular.module('yourAngularJsAppName') .factory('UsernameService', downgradeInjectable(UsernameService));
3. The filters in the project are gradually upgraded to the angular5 pipe, while the angular1.x filters are still retained.
Due to the performance problem of filter, filter has been changed to pipe in angular2. The angular team does not provide a module to upgrade filter to pipe, or downgrade pipe to filter, so filter is used in angular1.x. Using pipe, the filter upgrade is placed before the component, because the component template may be used username-pipe.ts:import { Pipe, PipeTransform } from '@angular/core'; Pipe({ name: 'username' }) export class usernamePipe implements PipeTransform { transform(value: string): string { return value === 'nina' ? '张三' : value; } }
import { Component, EventEmitter, Input, Output, ViewContainerRef } from '@angular/core'; import { UsernameService } from '../../service/username-service'; @Component({ selector: 'hero-detail', templateUrl: './hero-detail.component.html' }) export class HeroDetailComponent { Public hero: string; constructor(private usernameService: UsernameService) { this.hero = usernameService.get() } }
downgrade-components.ts:
import * as angular from 'angular'; import { downgradeComponent } from '@angular/upgrade/static'; import { HeroDetailComponent } from './app/components/hero-detail/hero-detail.component'; angular.module('yourAngularJsAppName') .directive('heroDetail', downgradeComponent({ component: HeroDetailComponent }) as angular.IDirectiveFactory)
5. Change the angular1.x controller to angular5 componen
tNow only the controller is left. Angular2 has canceled the controller. The controller can treat it as a large component, so we reconstruct the controller according to the component method and downgrade the new component. After the controller is reconstructed, we The routing needs to be modified. We are still using the routing of angular1. To learn more, go to the PHP Chinese websiteAngularJS Development Manual
).state('test', { url: '/test', controller: 'TestContentCtrl', controllerAs: 'vm', templateUrl: './src/controllers/test-content-ctrl.html' })
.state('test', { url: '/test', template: '<test-content></test-content>' })
6, third party Plug-in or library solution
Regarding plug-ins or libraries based on angular1.x that are referenced in the project, you can basically find the angular2 version. You can introduce the angular2 version for downgrade processing and then use it in angular1.x, but ~~~, angular2 Many APIs have been changed in the version, and the corresponding usage methods in angular1.x may no longer exist. Here are two solutions
Introduce the angular2 version, delete the angular1.x version, and downgrade After that, if the plug-in is used in the angular1. The x application uses the angular1. Option 2 is a good choice without affecting the loading time of the first screen, because going through all the APIs of all plug-ins or libraries at once is a relatively large workload and is prone to errors, and it is not in line with our original intention of incremental upgrade
-
Now that basically all the content in the project has been upgraded to angular5, we can delete the two files downgrade-services.ts and downgrade-components.ts, and upgrade the routing to angular5. Delete the libraries and plug-ins related to angular1. (Study in manual
), if you have any questions, you can leave a message below.
The above is the detailed content of Angular1.x and angular2+ run in parallel, angular1.x upgrade angular2+ solution. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

Real-time collaborative editors have become a standard feature of modern web development, especially in various team collaboration, online document editing and task management scenarios. Real-time communication technology based on WebSocket can improve communication efficiency and collaboration effects among team members. This article will introduce how to use WebSocket and JavaScript to build a simple online collaborative editor to help readers better understand the principles and usage of WebSocket. Understand the basic principles of WebSocketWebSo
