Where does DOM manipulation belong in Angular 2?

后端 未结 2 1746
予麋鹿
予麋鹿 2020-12-01 11:20

In Angular 1 all DOM manipulation should be done in directives to ensure proper testability, but what about Angular 2? How has this changed?

I\'ve been searching for

2条回答
  •  伪装坚强ぢ
    2020-12-01 11:31

    Based upon recommend solution by developers: http://angularjs.blogspot.de/2016/04/5-rookie-mistakes-to-avoid-with-angular.html

    @Component({
      selector: 'my-comp',
      template: `
        
    ` }) export class MyComp implements AfterViewInit { @ViewChild('myContainer') container: ElementRef; constructor() {} ngAfterViewInit() { var container = this.container.nativeElement; console.log(container.width); // or whatever } }

    Attention: The view child name has to begin with myName and in the template you need #.

提交回复
热议问题