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

Class Scope

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

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected