MCPcopy Create free account
hub / github.com/angular/angular / Scope

Class Scope

packages/compiler/src/render3/view/t2_binder.ts:292–553  ·  view source on GitHub ↗

* Represents a binding scope within a template. * * Any variables, references, or other named entities declared within the template will * be captured and available by name in `namedEntities`. Additionally, child templates will * be analyzed and have their child `Scope`s available in `childScope

Source from the content-addressed store, hash-verified

290 * be analyzed and have their child `Scope`s available in `childScopes`.
291 */
292class Scope implements Visitor {
293 /**
294 * Named members of the `Scope`, such as `Reference`s or `Variable`s.
295 */
296 readonly namedEntities = new Map<string, TemplateEntity>();
297
298 /**
299 * Set of element-like nodes that belong to this scope.
300 */
301 readonly elementLikeInScope = new Set<DirectiveOwner>();
302
303 /**
304 * Child `Scope`s for immediately nested `ScopedNode`s.
305 */
306 readonly childScopes = new Map<ScopedNode, Scope>();
307
308 /** Whether this scope is deferred or if any of its ancestors are deferred. */
309 readonly isDeferred: boolean;
310
311 private constructor(
312 readonly parentScope: Scope | null,
313 readonly rootNode: ScopedNode | null,
314 ) {
315 this.isDeferred =
316 parentScope !== null && parentScope.isDeferred ? true : rootNode instanceof DeferredBlock;
317 }
318
319 static newRootScope(): Scope {
320 return new Scope(null, null);
321 }
322
323 /**
324 * Process a template (either as a `Template` sub-template with variables, or a plain array of
325 * template `Node`s) and construct its `Scope`.
326 */
327 static apply(template: ScopedNode | Node[]): Scope {
328 const scope = Scope.newRootScope();
329 scope.ingest(template);
330 return scope;
331 }
332
333 /**
334 * Internal method to process the scoped node and populate the `Scope`.
335 */
336 private ingest(nodeOrNodes: ScopedNode | Node[]): void {
337 if (nodeOrNodes instanceof Template) {
338 // Variables on an <ng-template> are defined in the inner scope.
339 nodeOrNodes.variables.forEach((node) => this.visitVariable(node));
340
341 // Process the nodes of the template.
342 nodeOrNodes.children.forEach((node) => node.visit(this));
343 } else if (nodeOrNodes instanceof IfBlockBranch) {
344 if (nodeOrNodes.expressionAlias !== null) {
345 this.visitVariable(nodeOrNodes.expressionAlias);
346 }
347 nodeOrNodes.children.forEach((node) => node.visit(this));
348 } else if (nodeOrNodes instanceof ForLoopBlock) {
349 this.visitVariable(nodeOrNodes.item);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected