Skip to content

Supply context to NgTemplateOutlet #8368

@shlomiassaf

Description

@shlomiassaf

Current behavior
NgTemplateOutlet does not accept context.

Expected/desired behavior
NgTemplateOutlet has an additional @Input that allows attaching local context to EmbeddedViewRef

Other information
After feat(core): introduce template context it is relativly easy to supply a context to the template ref

Should be something like

import {Directive, Input, ViewContainerRef, ViewRef, TemplateRef} from 'angular2/core';
import {isPresent} from 'angular2/src/facade/lang';

/**
 * Creates and inserts an embedded view based on a prepared `TemplateRef`.
 *
 * ### Syntax
 * - `<template [ngTemplateOutlet]="templateRefExpression"></template>`
 */
@Directive({selector: '[ngTemplateOutlet]'})
export class NgTemplateOutlet {
  private _insertedViewRef: ViewRef;
  private _context: Object;

  constructor(private _viewContainerRef: ViewContainerRef) {}

  @Input()
  set ngTemplateOutletContext(context: Object) {
    this._context = context;
  }

  @Input()
  set ngTemplateOutlet(templateRef: TemplateRef<Object>) {
    if (isPresent(this._insertedViewRef)) {
      this._viewContainerRef.remove(this._viewContainerRef.indexOf(this._insertedViewRef));
    }

    if (isPresent(templateRef)) {
      this._insertedViewRef = this._viewContainerRef.createEmbeddedView(templateRef, this._context);
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions