首页 > 解决方案 > 如何在 Angular 7 中设置动态属性

问题描述

在 HTML 中:

<input class="form-control"[(ngModel)]="value" #muInput/>
<button (click)="onSetAttribute()"> set</button>`

在这:

`@ViewChild('muInput') muInput: ElementRef; `

public seperator : string onSetAttribute() { if(this.seperator) { this.muInput.nativeElement.setAttribute('mask' , this.seperator); this.muInput.nativeElement.setAttribute('thousandSeparator' , ',');` }}

我想点击按钮设置一个掩码属性来输入我的代码但不工作

标签: javascriptangulartypescriptdomattributes

解决方案


你可以这样做:

  <input class="form-control"[(ngModel)]="value" #muInput  [attr.mask]="mask" />
   <button (click)="changeMask()"> set</button>

在你的组件 ts 文件中,你应该声明一个类属性mask,然后给它你想要的 w/e 默认值:

changeMask() { this.mask = 'separator.2' // w/e value }

thousandSeparator属性也一样。


推荐阅读