【发布时间】:2017-08-07 15:16:00
【问题描述】:
为什么 ElementRef 使用起来不安全,如果是,我们可以用什么代替?
我一直在使用此 ElementRef 来查看或观看特定的 html 标记,然后在初始化后以特定宽度发送,但如果这会带来安全风险,我不会使用它,老实说我不会了解为什么 Angular 2 团队在他们的框架中允许这种安全漏洞。
什么是最安全和最好的技术?我的测试组件如下:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-standard',
template: ` <button type="button" #buttonW></button>`,
})
export class standardComponent implements OnInit {
name: string = 'app-standard';
viewWidthButton: any;
@ViewChild( 'buttonW' ) elButtonW: ElementRef;
constructor() {
this.viewWidthButton = this.elButtonW.nativeElement.offsetWidth;
console.log ('button width: ' + this.viewWidthButton);
}
ngOnInit() {
}
}
Angular 2 页面参考:
https://angular.io/docs/ts/latest/api/core/index/ElementRef-class.html
【问题讨论】:
-
链接上的推荐解决方案仍在使用 ElementRef,不确定是否安全
-
风险不是缺陷。并且风险不是由 Angular 引入的。它与
ElementRef本身无关。您的代码不会做任何有风险的事情。
标签: angular