有人知道如何获取组件模板中定义的元素吗?聚合物使得它真正用简单的$和$$.
我只是想知道如何在Angular中实现它.
以教程为例:
import {Component} from '@angular/core'
@Component({
selector:'display'
template:`
<input #myname(input)="updateName(myname.value)"/>
<p>My name : {{myName}}</p>
`
})
export class DisplayComponent {
myName: string = "Aman";
updateName(input: String) {
this.myName = input;
}
}
Run Code Online (Sandbox Code Playgroud)
如何从类定义中捕获por input元素的引用?
为什么ElementRef不安全,如果是这样,我们可以使用什么呢?
我一直在使用这个ElementRef来查看或观看特定的html标签,然后在初始化之后作为特定宽度发送,但是如果这打开了安全风险我将不会使用它,并且说实话我不明白为什么角度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() {
}
}
Run Code Online (Sandbox Code Playgroud)
Angular 2页面参考:
https://angular.io/docs/ts/latest/api/core/index/ElementRef-class.html
如何在TypeScript中的输入字段中选择文本.
在Javascript中这个wpuld看起来像这样:
document.getElementById('id').select();
Run Code Online (Sandbox Code Playgroud)
但TypeScript不支持select(错误:[ts]属性'select'在类型'HTMLElement'上不存在.)
添加了一些想法:它可能是一个问题beacouse我使用dx控件,他们创建一个包装器.你可以在这里看到html:
<dx-text-box _ngcontent-c1="" id="test" ng-reflect-disabled="false" ng-reflect-value="0:00" class="dx-texteditor dx-widget dx-textbox">
<div class="dx-texteditor-container">
<input autocomplete="off" class="dx-texteditor-input" type="text" spellcheck="false" tabindex="0" role="textbox">
<div data-dx_placeholder="" class="dx-placeholder dx-state-invisible"></div><div class="dx-texteditor-buttons-container">
</div></div></dx-text-box>
Run Code Online (Sandbox Code Playgroud)