[英]How to get ElementRef reference from NgModel FormControl in NgForm in Angular 4+
[英]How to get data attribute from angular FormControl
我有一個像下面這樣的表格
<form #f="ngForm">
<div >
<label
for="governmentalPosition">
MasterAgentInCashDeskGovernmentalPosition
</label>
<input
id="governmentalPosition"
name="governmentalPosition"
[attr.data-localization]="'MasterAgentInCashDeskGovernmentalPosition'"
type="text"
required
pInputText
maxlength="500"
#governmentalPositionControl="ngModel" [(ngModel)]="model.governmentalPosition">
</div>
</form>
<button type="button" (click)="save(f)" >
submit
</button>
我使用這個 ts 或 js 方法
save(f: NgForm) {
Object.keys(f.controls).forEach(controlName => {
let rr = f.controls[controlName].errors;
//this how I can get the attr.data-localization
}
}
如何在 angular 應用程序中獲取 ts 文件或 js 文件中的數據屬性
您可以使用不同的方法獲取data attribute
:
Angular方式:
@ViewChild('governmentalPositionControl') input: ElementRef;
//...
const dataValue = this.input.nativeElement.getAttribute("data-localization"); //--> get the data attribute
純 javascript :
const input = document.querySelector("#governmentalPosition"); //--> get the element
const dataValue = input.getAttribute("data-localization"); //--> get the data attribute
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.