X Tutup
Skip to content

Support for manual content reprojection #1989

@mhevery

Description

@mhevery

GOAL

  • provide <content>-like way of re-projecting content from one location of the tree to another.
  • Preserve the injection and context variables of the source

Implementation

Here is a proposed implementation skeleton.

  • make TemplateRef queryable (it already is injectable)

ng-outlet

@Directive({
  selector: '[ng-outlet]',
  properties: {
    'ngOutlet': 'ngOutlet',
    'ngOutletLocals': 'ngOutletLocals'
  },
  lifecycle: [onChange]
})
class NgOutlet {
  ngOutletLocals:any;
  ngOutlet:TemplateRef; // Maybe this should take an array of templates???

  constructor(public viewContainer: ViewContainerRef) { }

  onChange() {
    // todo: Don't destroy the view if ngTemplateRef did not change
    this.viewContainer.clear();
    var viewRef:ViewRef = this.viewContainer.createEmbeddedView(this.ngOutlet);
    // use structural changes to cal setLocal only if hash `ngOutletLocals` changed.
    for(var prop in this.ngOutletLocals) {
      viewRef.setLocal(prop, this.ngOutletLocals[prop]);
    }
  }
}

Example of Usage

@Component({
  selector: 'my-component'
})
@View({
  template: `
  <button template="var i = index" (click)="onClick()">
    {{i}}: {{text}}
  </button>

  <!-- here for simplicity, but it could be anywhere in the tree -->
  <template *ng-outlet="templates.first; locals={'index': 1}"></template>
`
})
class MyComponent {
  text: string;
  templates: QuerList<TemplateRef>;

  constructor(@ViewQuery(TemplateRef) templates: QueryList<TemplateRef>) {
    this.templates = templates;
  }

  onClick() {
    // do something
  }
}

Metadata

Metadata

Assignees

Labels

effort1: hoursfeatureLabel used to distinguish feature request from other issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    X Tutup