search
HomeWeb Front-endJS TutorialCompilation of Angular8+ interview questions: analysis of basic knowledge points

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>
  • 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)
  }
];

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
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
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.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment