Angular 2 : add hyphen after every 4 digit in input , card number input

后端 未结 2 1343
醉梦人生
醉梦人生 2020-12-22 01:37

i need to add an hyphen after every 4 digit i enter, i am getting this in the console , how can i can achieve this to change in the input in angular 2

Code i used gi

2条回答
  •  没有蜡笔的小新
    2020-12-22 01:53

    You need to do changes as below

    
    

    you don't need [(ngModel)] just keep [ngModel] as you are taking care of change event and from method do like this, you don't need self in angular this will be okay.

      mychange(val) {
        const self = this;
        let chIbn = val.split('-').join('');
        if (chIbn.length > 0) {
          chIbn = chIbn.match(new RegExp('.{1,4}', 'g')).join('-');
        }
        console.log(chIbn);
        this.iban_no = chIbn;
      }
    

    there is issue in your method too, you need to use val instead of property directly as you are trying to modify val as assigning value to property.

提交回复
热议问题