Table of Contents
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" >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" >angular1.x upgrade angular2 plan
Home Web Front-end JS Tutorial Angular1.x and angular2+ run in parallel, angular1.x upgrade angular2+ solution

Angular1.x and angular2+ run in parallel, angular1.x upgrade angular2+ solution

Sep 07, 2018 pm 05:33 PM
angular.js javascript typescript

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 });
  }
}
Copy after login
Use the AppModule.ngDoBootstrap method Start the AngularJS application. Now we can use the platformBrowserDynamic.bootstrapModule method to start the AppModule. main.ts:

import {AppModule} from './app/app.module';
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.log(err));
Copy after login
We are about to start running a hybrid application with AngularJS 5! All existing AngularJS code will work normally as before, but we can also run Angular code now

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'
  }
}
Copy after login
To use UsernameService in angular1.x, first create a downgrade-services.ts file, where all Services used in angular1.x after the angular5 service is downgraded

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));
Copy after login
After completing these two steps, UsernameService can be injected into angular1.x controller component service, etc. , the usage method in angular5 is not given here. Just follow the usage method of angular5.

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;
  }
}
Copy after login
4. Gradually upgrade the components in the project For the component

of angular5, we upgrade the content in hero-detail.js to hero-detail.ts:

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()
  }
}
Copy after login
To use the hero-detail component in angular1.x, First create a downgrade-components.ts file, which will store all the components used in angular1.x after downgrading angular5 components

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)
Copy after login
Now you can The hero-detail component is used in the template in angular1.x. The communication problem between components is written according to the interface of angular5

5. Change the angular1.x controller to angular5 componen

t

Now 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 website

AngularJS Development Manual

)

.state('test', {
  url: '/test',
  controller: 'TestContentCtrl',
  controllerAs: 'vm',
  templateUrl: './src/controllers/test-content-ctrl.html'
 })
Copy after login
After changing TestContentCtrl to test-content component
.state('test', {
  url: '/test',
  template: '<test-content></test-content>'
 })
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

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 implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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 implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

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.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

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 forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

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

How to implement an online collaborative editor using WebSocket and JavaScript How to implement an online collaborative editor using WebSocket and JavaScript Dec 17, 2023 pm 01:37 PM

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

See all articles