angular - ngStyle VS 渲染器2?我应该使用什么?

标签 angular ngfor ng-style angular-renderer2

我正在使用 Angular 5.2.9。

我想知道什么时候应该使用 Renderer2 而不是 ngStyle ?哪个是最佳解决方案?

1:<div #div>FOO BAR</div>

  @ViewChild('div') div: ElementRef;

  constructor(private renderer: Renderer2) {}

  ngAfterViewInit() {
      this.renderer.setStyle(this.div.nativeElement, 'background', 'blue');
  }

2:<div [ngStyle]="styleValue">FOO BAR</div>

  styleValue: any = {};

  ngAfterViewInit() {
      this.styleValue = {background: 'blue'};
  }

我知道在 ngFor 中使用“ngStyle”更容易,例如:

<div ngFor="let elem of array" [ngStyle]="styleValue">

否则你应该为这种情况做:<div ngFor="let elem of array" #div>FOO BAR</div>

  @ViewChildren('div') divs: QueryList<ElementRef>;

  constructor(private renderer: Renderer2) {}

  ngAfterViewInit() {
     this.divs.change.subscribe(() => {
        this.toFlipArray.forEach((div) => {
            this.renderer.setStyle(this.div.nativeElement, 'background', 'blue');
        })
     }
  }

在 ngFor 中使用 Renderer2 似乎要长得多,我什至没有终止订阅。

性能有区别吗?也许在其他地方?

最佳答案

两者都是 ngStylerenderer.setStyle用于动态设置组件样式

但是renderer.setStyle看起来优先于 [ngStyle]即使ngStyle看起来是一种嵌入式风格。

演示示例:

https://stackblitz.com/edit/angular-jtdk4z?file=src%2Fapp%2Fapp.component.html

When looking to the internal implementation of ngStyle:

https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_style.ts

it looks like it itself is implemented using renderer.setStyle

@Directive({selector: '[ngStyle]'})
export class NgStyle implements DoCheck {
  private _ngStyle: {[key: string]: string};
  private _differ: KeyValueDiffer<string, string|number>;

  constructor(
      private _differs: KeyValueDiffers, private _ngEl: ElementRef, private _renderer: Renderer2) {}

  @Input()
  set ngStyle(v: {[key: string]: string}) {
    this._ngStyle = v;
    if (!this._differ && v) {
      this._differ = this._differs.find(v).create();
    }
  }

  ngDoCheck() {
    if (this._differ) {
      const changes = this._differ.diff(this._ngStyle);
      if (changes) {
        this._applyChanges(changes);
      }
    }
  }

  private _applyChanges(changes: KeyValueChanges<string, string|number>): void {
    changes.forEachRemovedItem((record) => this._setStyle(record.key, null));
    changes.forEachAddedItem((record) => this._setStyle(record.key, record.currentValue));
    changes.forEachChangedItem((record) => this._setStyle(record.key, record.currentValue));
  }

  private _setStyle(nameAndUnit: string, value: string|number|null|undefined): void {
    const [name, unit] = nameAndUnit.split('.');
    value = value != null && unit ? `${value}${unit}` : value;

    if (value != null) {
      this._renderer.setStyle(this._ngEl.nativeElement, name, value as string);
    } else {
      this._renderer.removeStyle(this._ngEl.nativeElement, name);
    }
}

关于angular - ngStyle VS 渲染器2?我应该使用什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50193146/

相关文章:

javascript - Angular 2 : Enhance the performance of scroll event with a big ngFor List

javascript - 使用 ng-style 有什么缺点?

javascript - Angular :使用 ngFor 为段落设置不同的颜色

Angular 6 谷歌广告词转换

javascript - 以编程方式垂直调整 div 的大小

javascript - 使用单个数组 Angular c​​omponent.html 显示矩阵

angular - *ngIf 和 *ngFor 在同一元素上导致错误

javascript - ngStyle 适用于 ngFor 中的所有元素

angular - 如何在 Angular 2 中默认显示常见的页眉和页脚?

angular - 在 Angular 6 中禁用 mat-stepper 上已完成的步骤