Table of Contents
About Angular CLI
About angular compilation, the difference between AOT and JIT" >About angular compilation, the difference between AOT and JIT
Angular two-way binding
Three ways of Angular data binding
About angular’s ​​Module
What is a directive (Directive)
The difference between Promise and Observable
If you want to improve the performance of Angular
How to upgrade the Angular version
Home Web Front-end JS Tutorial Compilation of Angular8+ interview questions: analysis of basic knowledge points

Compilation of Angular8+ interview questions: analysis of basic knowledge points

Feb 15, 2022 am 10:58 AM
angular Interview questions

This article will share with you some interview questions based on Angular8, and give you an in-depth understanding of the basic knowledge points of Angular8. I hope it will be helpful to everyone!

Compilation of Angular8+ interview questions: analysis of basic knowledge points

Related recommendations: 2022 Big Front-End Interview Questions Summary (Collection)

About Angular CLI

Angular CLI, also known as Angular scaffolding, is used to quickly generate the framework of a project or component to improve efficiency. Angular apps, components, services, etc. can be easily generated, and can be created according to your own needs through parameters. It can be said to be an indispensable tool for angular development. [Related tutorial recommendations: "angular tutorial"]
Reference: https://cli.angular.io/

  • ng generate: create new component, service, pipe, class etc
  • ng new: Create a new angular app
  • ng update: Upgrade angular itself and its dependencies
  • ng version: Display the anuglar cli global version and the local angular cli, Versions of angular code, etc.
  • ng add: Add a new third-party library. It will do two things, 1) install node_modules based on npm, 2) automatically change the configuration file to ensure that new dependencies work properly

About angular's dependency injection (dependency injection)

Dependency injection is an application design pattern implemented by Angular and is one of the core concepts of Angular.

A dependency is a service with a series of functions. Various components and directives (derictives) in the application may require the functions of the service. Angular provides a smooth mechanism through which we can inject these dependencies into our components and directives. So we're just building dependencies that can be injected between all the components of the application.

Using dependency injection also has the following benefits,

  1. No need to instantiate (new instance). You don’t need to care about what parameters are required in the constructor of the class
  2. Once injected (the app module is injected through Providers), all components can be used. Moreover, the same service instance (Singleton) is used, which means that the data in a service is shared and can be used for data transfer between components.

About angular compilation, the difference between AOT and JIT

Every Angular application contains components that the browser cannot understand and templates. Therefore, all Angular applications need to be compiled before running inside the browser.

Angular provides two compilation types:

  • JIT(Just-in-Time) compilation
  • AOT(Ahead-of-Time) compilation

The difference is that in JIT compilation, the application is compiled inside the browser at runtime, while in AOT compilation, the application is compiled during build time.
Obviously, AOT compilation has many benefits, so it is Angular’s ​​default compilation method. Key Benefits

  • Because the application is compiled before running inside the browser, the browser loads the executable code and renders the application immediately, resulting in faster rendering.
  • In AOT compilation, the compiler will send external HTML and CSS files along with the application, thereby eliminating separate AJAX requests for those source files, thus reducing ajax requests.
  • Developers can detect and handle errors during the build phase, which helps minimize errors.
  • The AOT compiler adds HTML and templates to JS files before running them in the browser. Therefore, there are no redundant HTML files to read, providing better security to the application.

Angular two-way binding

The principle of Angular two-way binding

Angular two-way binding, through Dirty checking is implemented.

  • The basic principle of dirty value detection is to store the old value, and when detecting, compare the new value at the current moment with the old value. If they are equal, there is no change. Otherwise, a change is detected and the view needs to be updated.

  • Zone.js is included in angular2. For setTimeout, addEventListener, promise, etc. are all executed in ngZone (in other words, they are encapsulated and rewritten by zone.js). Angular sets up the corresponding hook in ngZone, notifies angular2 to do the corresponding dirty check processing, and then updates DOM.

Angular two-way binding efficiency issue

For situations where an extremely large number of DOM elements need to be bound in the page (into Hundreds or thousands), you will inevitably encounter efficiency problems. (The specifics also depend on PC and browser performance). In addition, if the dirty check exceeds 10 times (experience value?), it is considered that there is a problem with the program and no more checks will be performed.
You can avoid this in the following ways

  • For data that is only used for display, use one-way binding instead of two-way binding;

  • Angular's data flow is top-down, flowing in one direction from parent components to child components. Unidirectional data flow ensures efficient and predictable change detection. Therefore componentization is also a means to improve performance.

  • Write less complex logic in expressions (and functions called by expressions)

  • Do not connect pipes that are too long ( Often pipe will traverse and generate new arrays. Pipe is called filter in anglarJS (v1))

  • Change detection strategy onPush. Angular has two change detection strategies. Default is Angular's default change detection strategy, which is the dirty check mentioned above (check all values ​​as long as they change). Developers can set up a more efficient change detection method based on the scenario: onPush. The onPush strategy means that the component will only perform change detection when the reference to the input data changes or an event is triggered.

  • NgFor should be used with the trackBy equation. Otherwise, NgFor will update the DOM for each item in the list during each dirty value detection process.

Three ways of Angular data binding

<div>
    <span>Name {{item.name}}</span>  <!-- 1. 直接绑定 -->
    <span>Classes {{item | classPipe}}</span><!-- 2. pipe方式-->
    <span>Classes {{classes(item)}}</span><!-- 3.绑定方法调用的结果 -->
</div>
Copy after login
  • Direct binding: In most cases, this is the best way to perform .

  • The result of the binding method call: During each dirty value detection process, the classes equation must be called once. If there are no special needs, this method of use should be avoided as much as possible.

  • pipe method: It is similar to the binding function. Every time the dirty value detection classPipe is called. However, Angular has optimized the pipe and added caching. If the item is equal to the last time, the result will be returned directly.

For more optimization tips, refer to the performance optimization tips in angular binding (dirty checking)

About angular’s ​​Module

What is Angular's Module

Module is a place where we can group components, services and pipes. Modules decide whether other modules can use components, directives, etc. by exporting or hiding these elements. Each module is defined using the @NgModule decorator.

The difference between Root Module and Feature Module.

Each Angular application can only have one root module (Root Module), and it can have one or more feature modules (Feature Module). The root module imports BrowserModule, while the function module imports CommonModule.

Module Lazy-loading

When a project is very large, in order to improve the loading speed of the first screen, you can pass Lazy-loading, when certain specific URLs are accessed, only those uncommon feature modules are loaded.

Implementation: Create feature module normally and modify routing configuration. For example:

const routes: Routes = [
  {
    path: &#39;customers&#39;,
    loadChildren: () => import(&#39;./customers/customers.module&#39;).then(m => m.CustomersModule)
  }
];
Copy after login

In this way, after compilation, this feature module will be an independent js. Only when the user accesses the url (~/customers), this independent js will be requested from the server and then loaded. ,implement.

Reference https://angular.io/guide/lazy-loading-ngmodules

What is a directive (Directive)

Directive (Directive) is used to add behavior to an existing There are elements (DOM) or components (Component).
At the same time, multiple instructions can be applied to an element or component.

The difference between Promise and Observable

First of all, the new version of anuglar recommends using Observable (belonging to RxJS). Secondly, for Observable objects, you can use .toPromise() to convert them into Promise objects.

  • Promise, regardless of whether then is called. Promises are executed immediately; observables are only created and executed when called (subscribe).

  • Promise returns a value; Observable returns 0 to N values. So the operator corresponding to Promise is .then(), and the corresponding operator to Observable is .subscribe

  • Observable, which also supports map, filter, reduce and similar operators

  • Observable can be canceled, but Promise cannot

If you want to improve the performance of Angular

Angular is still a web application, so The general techniques for improving web page performance are universal. For Angular, there are some special optimization techniques:

  • AOT compilation. As mentioned before, don't compile on the client side
  • The application has been minimized (uglify and tree shaking)
  • Remove unnecessary import statements. If there are any leftovers, they will also be included when packing.
  • Make sure that unused third-party libraries have been removed from the application. Same as above.
  • When the project is large, consider lazy loading (Lazy Loading) to ensure the loading speed of the homepage.

How to upgrade the Angular version

Angular CLI provides an upgrade command (ng update). At the same time, the official website (https://update.angular.io/) also has an upgrade guide. After selecting which version to upgrade from, step-by-step upgrade commands will be given, just execute them directly.

For more programming-related knowledge, please visit: Programming Learning! !

The above is the detailed content of Compilation of Angular8+ interview questions: analysis of basic knowledge points. 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

Video Face Swap

Video Face Swap

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

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 install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

Five common Go language interview questions and answers Five common Go language interview questions and answers Jun 01, 2023 pm 08:10 PM

As a programming language that has become very popular in recent years, Go language has become a hot spot for interviews in many companies and enterprises. For beginners of the Go language, how to answer relevant questions during the interview process is a question worth exploring. Here are five common Go language interview questions and answers for beginners’ reference. Please introduce how the garbage collection mechanism of Go language works? The garbage collection mechanism of the Go language is based on the mark-sweep algorithm and the three-color marking algorithm. When the memory space in the Go program is not enough, the Go garbage collector

Detailed explanation of angular learning state manager NgRx Detailed explanation of angular learning state manager NgRx May 25, 2022 am 11:01 AM

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

An article exploring server-side rendering (SSR) in Angular An article exploring server-side rendering (SSR) in Angular Dec 27, 2022 pm 07:24 PM

Do you know Angular Universal? It can help the website provide better SEO support!

How to use PHP and Angular for front-end development How to use PHP and Angular for front-end development May 11, 2023 pm 04:04 PM

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

A brief analysis of how to use monaco-editor in angular A brief analysis of how to use monaco-editor in angular Oct 17, 2022 pm 08:04 PM

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

A brief analysis of independent components in Angular and see how to use them A brief analysis of independent components in Angular and see how to use them Jun 23, 2022 pm 03:49 PM

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

What should I do if the project is too big? How to split Angular projects reasonably? What should I do if the project is too big? How to split Angular projects reasonably? Jul 26, 2022 pm 07:18 PM

The Angular project is too large, how to split it reasonably? The following article will introduce to you how to reasonably split Angular projects. I hope it will be helpful to you!

See all articles