Reference Core

作者 angular97e6aa4a5579無授權條款101K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫今天更新

Explains the mental model and architecture of the code under `packages/core`. You MUST use this skill any time you plan to work with code in `packages/core`

AI 產生的概覽

說明 Angular packages/core 執行時期的架構與心智模型,供在該程式庫中工作的開發者參考。

功能
此技能提供一份參考文件,說明 Angular packages/core 的架構,涵蓋 Ivy 渲染引擎、相依性注入、變更偵測與反應式。它解釋 LView 與 TView、渲染週期、注入器類型以及重要原始碼目錄等關鍵概念。文件也列出慣例、注意事項以及修改核心程式碼的步驟。
適用情境
在規劃或執行 packages/core 內程式碼相關工作時使用,例如修改執行時期指令或新增狀態。它用於在改動 Angular 核心執行時期之前,協助開發者建立整體認識。
執行需求
無需指令碼或工具,僅為純說明性參考文件。它假定使用者熟悉 Angular 程式庫及其 packages/core 目錄。

Angular Core (packages/core) Mental Model

This document outlines the architecture and mental model for packages/core, the heart of the Angular framework.

1. High-Level Architecture

packages/core contains the runtime logic for Angular. Its primary responsibilities are:

  1. Rendering (Ivy/Render3): Transforming templates into DOM updates.
  2. Dependency Injection (DI): Managing object creation and lifetime.
  3. Change Detection: Synchronizing the model with the view.
  4. Reactivity: Signals and Zone.js integration.

2. Rendering Engine (Ivy / Render3)

The rendering engine (located in packages/core/src/render3) uses an instruction-based approach.

Key Concepts

  • Instructions: The Angular compiler transforms templates into a sequence of instruction calls (e.g., ɵɵelementStart, ɵɵtext, ɵɵproperty). These instructions are executed at runtime to create and update the view.

    • Location: packages/core/src/render3/instructions
  • LView (Logical View): An array containing the state of a specific view instance. It holds:

    • DOM nodes (RElement, RText).
    • Binding values (for change detection).
    • Directive/Component instances.
    • Context: packages/core/src/render3/interfaces/view.ts
  • TView (Template View): An object describing the static structure of a view; its parallel array is TView.data. It is shared across all instances (LViews) of the same component/template. It holds:

    • Property names for bindings.
    • Node relationship information.
    • Compiled directive definitions.
    • Context: packages/core/src/render3/interfaces/view.ts
  • Memory Layout: LView and TView.data are parallel arrays. Index i in LView corresponds to metadata at index i in TView.data.

    • HEADER: Fixed size, contains context (Parent, Host, etc.).
    • DECLS: Static nodes (elements, text, pipes).
    • VARS: Binding values.
    • EXPANDO: Dynamic data (host bindings, injectors).

The Render Cycle

  1. Creation Mode: Instructions create DOM nodes and store them in LView.
  2. Update Mode: Instructions check current values against previous values stored in LView. If changed, they update the DOM.

3. Dependency Injection (DI)

DI in Angular is hierarchical and split into two systems that interact:

Module Injector (R3Injector)

  • Configured via @NgModule.providers or providedIn: 'root'.
  • Stored in a hierarchy of R3Injector instances.
  • Location: packages/core/src/di/r3_injector.ts

Node Injector

  • Configured via @Component.providers or @Directive.providers.
  • Not a class, but a data structure embedded in the LView ("Expando" section).
  • Uses Bloom Filters (TView.data) to quickly check if a token is present at a specific node index before traversing up the tree.
  • Resolves tokens starting from the current node, walking up the view tree (Element Injector hierarchy), and falling back to the Module Injector if not found.

4. Change Detection

  • Dirty Checking: Angular checks if values bound in templates have changed.
  • Strategies:
    • OnPush: The default. Checks when inputs change, events fire, template signals update, or the view is explicitly marked for checking.
    • Eager: Checks whenever the change detection traversal reaches the component. (Default is the deprecated alias for Eager.)
  • Signals: The new reactivity primitive. Signals notify the scheduler when they change, potentially allowing for fine-grained updates (Zoneless).

5. Key Directories to Know

  • src/render3: The Ivy rendering engine.
    • instructions: The runtime instructions called by compiled code.
    • interfaces: LView, TView, TNode definitions.
  • src/di: Dependency injection system.
  • src/change_detection: Change detection logic.
  • src/zone: Zone.js integration.
  • src/render3/reactivity: Signals implementation (signal, computed, effect), built on the reactive graph in primitives/signals.

6. Conventions & Gotchas

  • Prefixes: Private/Internal exports often start with ɵ.
  • Global State: Ivy relies heavily on global state (e.g., getLView()) during instruction execution to avoid passing context arguments everywhere. This is for performance and code size.
  • Performance: The code is highly optimized for performance and memory. You will see arrays used instead of objects, bitmasks, and manual memory management patterns. Respect these patterns.

7. How to Modify Core

  1. Understand the Instruction: If modifying runtime behavior, find the corresponding instruction in src/render3/instructions.
  2. Check LView/TView Impact: If adding state, understand where it fits in the LView array.
  3. Tests: Core has extensive tests. Run them using Bazel.

來源與署名

來源:angular/angular位於.agent/skills/reference-core提交97e6aa4

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架