search
HomeWeb Front-endJS TutorialQuickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

This article mainly introduces you to the quick start of angularjs5, so that everyone can quickly become familiar with new knowledge instead of writing code in the old version. Let’s take a look at this article together

1. Overview

Although it is called Angular5, in fact it is only the fourth version of this front-end framework that was born in 2012:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

It seems that one will be released in almost half a year New version, but actually starting from the rewritten version 2, the development interface and core ideas have stabilized, and basically maintain compatibility with the previous version.

In this new version of 5, the Angular team focuses its improvements on the following features:

  • Update Easily build progressive Web apps - __P__progressive __W__eb __A__pp

  • Use the build optimizer to weed out dead code for smaller apps and faster network load times

  • Making materialized design components compatible with server-side rendering

PWA is one proposed by Google The standard aims to provide web applications with a user experience comparable to native applications on mobile terminals. A PWA application mainly uses Service Worker and browser cache to improve the interactive experience. It can not only be deployed directly on the mobile phone desktop, but also can be applied offline:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

2. Introduce the angular environment

Angular It is recommended to use TypeScript to develop applications , which requires using an online compiler (JIT) to compile the code in real time, or using a precompiler (AOT) to compile the code in advance during the development period.

In order to avoid this cumbersome process from affecting thinking about the essence of the Angular framework, we have made the necessary configuration and packaging of these necessities to adapt to online writing and experimentation. Now you only need to introduce a library a5-loader.

The following figure shows the structure of the library. The blue components are all packaged in the library:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

You may notice Angular The frame is not blue. Indeed, we do not package it in a5-loader, but let the module loader (SystemJS) automatically load it according to the needs of the application. The purpose of this is to make the application code consistent with the backend construction method used in subsequent courses.

If you are interested in this library, you can visit the http://github.com/hubwiz/a5-l... warehouse on github.

3. Create Angular components

Angular is a component-oriented front-end development framework. If you have been engaged in the development of C/S graphical applications, you should know the meaning of the word component. Basically, components represent some program units with a graphical interface and inherent logical capabilities. The following figure lists three components used to implement ping-pong switching:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

components provide good reusability in a bunch of components On the basis of , we can achieve quite complex interactive functions using simple glue code.

Now let us create the

Angular component. The code is quite simple:

@Component({
  selector: "ez-app",
  template: `<h1 id="Hello-angular">Hello,angular5</h1>`
})
class EzComp{}
In the

Angular framework, __component__ refers to a A class with the Component decorator applied. ComponentThe function of the decorator is to append metadata information to the decorated class:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

##Angular

When the framework compiles and guides the application, it will use these metadata to construct the view. Two of the metadata are very important:

    __selector__: the
  • CSS

    selector of the component host element, which declares the component's rendering anchor point in the DOM tree

  • __template__: The template of the component, the framework will use this template as a blueprint to build the view
4. Create the Angular module

Angular

The core of the framework is componentization, and its design goal is to adapt to the development of large-scale applications. Therefore, the concept of module (NgModule) is introduced in application development to organize different components (and services). An Angular application needs to create at least one module. In order to distinguish it from the module concept of the JavaScript language itself, __NG module__ will be used in this course to represent an Angular module.

类似于组件,NG模块就是一个应用了NgModule装饰器的类。例如,下面的代码创建了一个NG模块EzModule

@NgModule({
  imports: [ BrowserModule ],
  declarations: [ EzComp ],
  bootstrap: [ EzComp ]
})
class EzModule{}

同样,NgModule装饰器用来给被装饰的类附加模块元数据,可以查看被装饰类的__annotations__属性来观察这一结果:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

NgModule装饰器声明了一些关键的元数据,来通知框架需要载入哪些NG模块、编译哪些组件以及启动引导哪些组件:

  • __imports__: 需要引入的外部NG模块

  • __declarations__:本模块创建的组件,加入到这个元数据中的组件才会被编译

  • __bootstrap__:声明启动引导哪个组件,必须是编译过的组件

需要强调的是,bootstrap元数据声明的组件必须是编译过的组件:它要么属于使用imports元数据引入的外部NG模块,要么是已经在declarations元数据中声明的本地组件。

NG模块BrowserModule定义于包@angular/platform-browser,它是Angular跨平台战略的重要组成部分。BrowserModule封装了浏览器平台下的核心功能实现,与之对应的其他平台实现还有:

  • ServerModule:服务端实现

  • WorkerAppModule:WebWorker实现

通常情况下开发Web应用时,我们都需要引入BrowserModule这一NG模块。

五、启动Angular应用

前面课程中,我们已经创建了一个组件和一个NG模块,不过似乎只是定义了一堆的元数据,几乎没有写太多有价值的代码。
但这就是Angular框架的一个特点:__声明式开发__。这些元数据是用来向框架声明如何引导启动应用程序的重要信息。

启动代码很简单,引入platformBrowserDynamic()工厂函数、创建平台实例、启动指定模块:

<br>import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"<br>const pref = platformBrowserDynamic()<br>pref.bootstrapModule(EzModule)<br>

√ 平台对象:PlatformRef

platformBrowserDynamic()函数返回一个PlatformRef对象(Angular对平台的抽象),这个函数最重要的作用,在于其内部创建了一个即时(__J__ust __I__n __T__ime)编译器,可以在线实时编译NG模块和组件,这也是它被称为动态(Dynamic)的原因:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

平台对象的bootstrapModule()方法用来启动指定的NG模块,启动的绝大部分工作,在于利用JIT编译器编译NG模块和组件,当这些编译工作都完成后,则根据启动模块的bootstrap元信息,渲染指定的组件。

六、为什么这么复杂?

可能你已经感觉有点复杂了:只是为了写一个Hello,World,就要写这么多代码。

事实上这些复杂性是随着Angular的发展逐步引入的,从好的一方面说,是提供给开发者的可选项逐渐增多了,适用场景变多了。

比如,在Angular2正式版之前,都没有NG模块的概念,你只要写一个组件就可以直接启动应用了。Angular团队的预期应用场景是大规模前端应用开发,因此显式的NG模块声明要求也是容易理解的。不过即使是小型的应用,由于可以只使用一个NG模块,因此这一点的复杂性增加倒也不多,只是增加了学习和运用这个新概念的成本。

另一个显而易见的复杂性,在于多平台战略的引入。Angular希望让应用可以跨越浏览器、服务器等多个平台(基本)直接运行。因此免不了抽象一个中间层出来,我们需要在应用中显式地选择相应的平台实现模块:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

The third complexity comes from support for precompilation (AOT: Ahead Of Time). In the early days, Angular only had just-in-time compilation (JIT: Just In Time), which means that the application code was compiled at runtime. The first problem with just-in-time compilation is that the compiler code needs to be packaged in the application, which increases the
size of the final published application code; another problem is that compilation takes time, which increases the waiting time for users to open the application. Therefore, Angular currently supports both JIT and AOT, but the application that starts JIT compilation and the application that starts AOT compilation currently need to be explicitly selected:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

For Angular, compilation converts the entry NG module definition into an NG module factory (NgModuleFactory). For JIT, this step is implicit in bootstrapModule(). For AOT, generating the module factory is over. Just use bootstrapModuleFactory() to call the generated module factory when the application starts.

Although AOT compilation is usually used during the build phase, we can simulate this two-step process in the browser. (If you want to see more, go to the PHP Chinese website AngularJS Development Manual to learn)

7. Understand the original intention of Angular

In addition to the framework itself Complexity caused by powerful functions. Another source of complexity of Angular lies in its highly encapsulated declarative API, which makes it difficult for developers to figure out and gain insight into the implementation mechanism of the framework. Therefore, they feel guilty when using it. Once a problem occurs, it is difficult to analyze and troubleshoot. :

Angular cannot be used as a black box.

On the one hand, the reason is that Angular provides API development interfaces with its declarative template syntax as the core. The templates written by developers go through quite complex compilation processing by the framework before rendering the final View object. If you don't try to understand what happens in the process from template to view object, I believe you will always feel like you are out of control.

On the other hand, the reason is that Angular is a framework, which sets up the application framework and leaves some gaps for developers to fill. It's difficult to take full advantage of a framework without understanding as much as possible about how it works.

The starting point for developing Angular is to use HTML to write user interfaces. Think about how easy it is to develop a static web page, and you will know what a good idea this is:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

The problem with native HTML is that, first of all, it requires JavaScript to achieve decent user interaction, and secondly, it only has so many tags available, making it difficult to take on the task of developing a user interface.

Since the browser cannot directly interpret tags like <ez-gauge></ez-gauge>, the Angular team introduced the concept of a compiler:
Before sending it to the browser, first HTML with extension tags is translated into native HTML supported by the browser:

Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4

Okay, this article is over ( If you want to see more, go to the PHP Chinese website AngularJS User Manual to learn). If you have any questions, you can leave a message below.

The above is the detailed content of Quickly start development with Angular 5, say goodbye to Angular.js, Angular 2, and Anuglar 4. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor