<elementRef>

import {ElementRef} from "@angular/core";

 

constructor(private element: ElementRef) {
   // now, we can reference to: this.element
}

 

this.element.nativeElement('.js-banner-container'),

 

ElementRef

Provides access to the underlying native element (DOM element).

 

import {AfterContentInit, Component, ElementRef} from '@angular/core';

@Component({
  selector: 'app',
  template: `
  <h1>My App</h1>
  <pre style="background: #eee; padding: 1rem; border-radius: 3px; overflow: auto;">
    <code>{{ node }}</code>
  </pre>
`
})
export class App implements AfterContentInit {
  node: string;

  constructor(private elementRef: ElementRef) {
  }

  ngAfterContentInit() {
    const tmp = document.createElement('div');
    const el = this.elementRef.nativeElement.cloneNode(true);

    tmp.appendChild(el);
    this.node = tmp.innerHTML;
  }

}

参考这篇 Angular 2: @Directive() 指令创建无限滚动

posted on 2016-12-30 15:49  meeming  阅读(271)  评论(0编辑  收藏  举报



Fork me on GitHub