angular 在模板中申明临时变量(指令实现)

指令代码

import { Directive, ViewContainerRef, TemplateRef, Input } from '@angular/core';

@Directive({
  selector: '[let]'
})
export class ExpAsDirective {
  _context: any = {};
  @Input() public set letContext(val) {
    this._context = val;
    this._updateView();
  }
  constructor(private _viewContainer: ViewContainerRef, templateRef: TemplateRef) {
    this._thenTemplateRef = templateRef;
  }
  private _updateView() {
    this._viewContainer.clear();
    this._viewContainer.createEmbeddedView(this._thenTemplateRef, this._context);
  }
}

使用

<div *let="let tt1 = tt;let age1 = age;context: {tt: name, age: 12}">{{tt1}}-------{{age1}}div>
<input [(ngModel)]="name">

和ngTemplateOutlet的使用方法相同

你可能感兴趣的:(angular2/4)