javascript - 如何在 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' , ',');` }}
我想点击按钮设置一个掩码属性来输入我的代码但不工作
解决方案
你可以这样做:
<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
属性也一样。
推荐阅读
- java - Hibernate 在更新现有父实体时不会创建新的子实体
- sql-server - 如何识别 MS SQL 数据库表中的拼音字母
- reactjs - 使用useState设置状态时,没有prevState的箭头函数和准备好的数据有什么区别
- java - 为什么 webdriver unix 可执行文件在 maven 构建上转换为 textedit 文件?
- sql - 删除和截断和表大小
- java - 在 HttpSecurityConfig 上使用访问控制时,WithMockUser 不起作用
- angular - 身份服务器 4:强制用户在关闭浏览器/选项卡后使用提供者登录
- mongodb - 在不同的树层上有唯一的 mongodb 索引
- vue.js - nginx:当 root 是变量时,try_files 的最后一个值不起作用
- python - 如何将触发器类型作为有效负载传递给云构建中的“方法:projects.triggers.create”