在输入文本中添加百分比?Angular

xyhw6mcr  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(330)

我需要在输入文本中添加百分比符号。
我所需要的就是这个(相同)
https://codepen.io/mesapkota/pen/bmoqzr
但在Angular 上。没有jquery。
这是我的项目
https://stackblitz.com/edit/percentage-directive-in-angular?file=src/app/app.component.html

<input type="textbox" [(ngModel)]="InputValue" placeholder="example 22.2" appPercentageDirective>
798qvoo8

798qvoo81#

您需要将keyup添加到输入中,每次将“%”替换为“”,然后再次添加。我认为您的代码应该如下所示:app.component.html

<p>
  Percentage Directive <br/>
  <b>Example: </b> 0 to 100
</p>
<input type="textbox"  #box (keyup)="setvalue(box.value)"[(ngModel)]="InputValue" placeholder="example 22.2" >
 %

app.component.ts:

setvalue(value) {
 this.InputValue = value.replace('%','')+"%"    
  }
whlutmcx

whlutmcx2#

我认为直接添加%是没有意义的,您可以在请求时添加它,如果您想显示页面,可以在css显示中使用伪元素,因此代码将更加灵活

相关问题