如何在angular6中将一个文本框值填充到另一个文本框

时间:2019-04-05 15:32:35

标签: angular

seat1Modified(value): void {
    value.seat2 = value.seat1;
    value.seat3 = value.seat2;
  }
  seat2Modified(value): void {
    value.seat3 = value.seat2;  
  }
<div class="col-md-1"> <input type="number" [(ngModel)]="vlaue.seat1" (ngModelChange)="seat1Modified(vlaue)"/> </div> <div class="col-md-1"> Year 2 seats </div> <div class="col-md-1"> <input type="number" [(ngModel)]="vlaue.seat2" (ngModelChange)="seat2Modified(vlaue)" /> </div> <div class="col-md-1"> Year 3 seats </div> <div class="col-md-1"> <input type="number" [(ngModel)]="vlaue.seat3" /> </div>

enter image description here

如果输入第一个文本框值,则需要填充第二个文本框,如果输入第二个文本框值,则填充第三个文本框

     2年级      3年级   

1 个答案:

答案 0 :(得分:0)

您的变量名中有一些拼写错误(vlaue在模板中应为value

检查此:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  value = {
    seat1: undefined,
    seat2: undefined,
    seat3: undefined,
  }
  seat1Modified(value): void {
    value.seat2 = value.seat1;
    value.seat3 = value.seat2;
  }
  seat2Modified(value): void {
    value.seat3 = value.seat2;  
  }
}

HTML

<div class="col-md-1"> 
  <input type="number" [(ngModel)]="value.seat1" (ngModelChange)="seat1Modified(value)"/>
</div> 

<div class="col-md-1"> Year 2 seats </div> 

<div class="col-md-1"> 
  <input type="number" [(ngModel)]="value.seat2" (ngModelChange)="seat2Modified(value)" /> 
</div> 
<div class="col-md-1"> Year 3 seats </div> 
<div class="col-md-1"> <input type="number" [(ngModel)]="value.seat3" /> </div>

工作stackblitz