使用ViewChild和Renderer2有什么区别?

时间:2018-08-04 15:33:12

标签: angular6

我试图了解是使用ViewChild还是Renderer2进行DOM操作以及两者之间的区别。我想我可以使用ViewChild来访问元素,如下所示,但是我想这不是获取DOM中元素的最佳方法,因为我们没有使用Angular的抽象。

import { Component } from '@angular/core';
import { ViewChild } from "@angular/core/src/metadata/di";

@Component({
  selector: 'app-root',
  templateUrl: '<input type="text" #input>'
})
export class AppComponent {
  @ViewChild("input") input;

  ngAfterContentInit() {
    this.input.nativeElement.focus();
  }
}

相反,我应该使用Renderer2,但不幸的是,我不知道如何使用Renderer2编写与上面相同的代码。

0 个答案:

没有答案