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

Class Scope

packages/compiler/src/render3/view/t2_binder.ts:295–555  ·  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

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected