简体   繁体   English

如何使用 Angular 中的模板引用为输入文本字段赋值 8

[英]How to assign value to the input text field using template reference in Angular 8

I have drop down change event, during the change event I need populate some value to the respective text field.我有下拉更改事件,在更改事件期间我需要将一些值填充到相应的文本字段。

app.component.html app.component.html

<tr>
  <td>John</td>
  <td>
    <select (change)="editAge(selectField1.value, 1)" #selectField1>
      <option value="1">Less than 10</option>
      <option value="2">Greater than 10 and Less than 80</option>
      <option value="3">Less than 80</option>
    </select>
  </td>
  <td>
    <span *ngIf="!(selectField1.value == 2)">24</span>
    <span *ngIf="selectField1.value == 2">
      <input type="text" #textField1/>
    </span>
  </td>
</tr>
<tr>
  <td>Jacky</td>
  <td>
    <select (change)="editAge(selectField2.value, 2)" #selectField2>
      <option value="1">Less than 10</option>
      <option value="2">Greater than 10 and Less than 80</option>
      <option value="3">Less than 80</option>
    </select>
  </td>
  <td>
    <span *ngIf="!(selectField2.value == 2)">4</span>
    <span *ngIf="selectField2.value == 2">
      <input type="text" #textField2 />
    </span>
  </td>
</tr>

app.component.ts应用程序组件.ts

  expression = false;
  nameBind: string;
  @ViewChild('textField') nameInputRef: ElementRef;
  editAge(ee, i) {
    this.nameInputRef.nativeElement.value = 'Apple';
  }

During the change event editAge I need to update the respective row text field.在更改事件editAge期间,我需要更新相应的行文本字段。 How to get the dynamic input template and update it?如何获取动态输入模板并更新它?

Example例子

Since you need to change input of each column of the row.由于您需要更改行的每一列的输入。 it is better to handle it via models instead of html element reference.最好通过模型而不是 html 元素引用来处理它。

<table border="1">
  <tr><td>Name</td><td>Age</td><td>New Age</td></tr>
  <tr *ngFor="let cust of data; let j = index;"><td>{{cust.name}}</td><td>
    <select (change)="editAge(cust.age, j)" 
    [(ngModel)]="cust.age"
    #selectField1>
    <option [value]="i+1" *ngFor="let age of dropdownList; let i = index;">{{age.label}}</option>
  </select></td><td> <span  *ngIf="!(selectField1.value == 2)">{{cust.newAge}}</span> <span *ngIf="selectField1.value == 2"><input type="text" [(ngModel)]="cust.newAge" #textField/></span></td></tr>
</table>

dropdownList: any = [
    { label: 'Less than 10', newAge: '' },
    { label: 'Greater than 10 and Less than 80', newAge: 'Apple' },
    { label: 'Less than 80', newAge: 'Banana' }
  ];

  data: any = [
    { name: 'John', age: '1', newAge: '24' },
    { name: 'Jacky', age: '1', newAge: '4' },
    { name: 'Roy', age: '1', newAge: '34' }
  ]
  editAge(ee, i) {
    this.data[i].newAge = this.dropdownList[ee-1].newAge;
  }

checkout this demo https://stackblitz.com/edit/angular-styrlp and let me know if it resolves.查看此演示https://stackblitz.com/edit/angular-styrlp并让我知道它是否已解决。

updated (dynamic text field)更新(动态文本字段)

@ViewChild('textField1', { static: false }) nameInputRef1: ElementRef;
  @ViewChild('textField2', { static: false }) nameInputRef2: ElementRef;
  @ViewChild('textField3', { static: false }) nameInputRef3: ElementRef;

editAge(ee, i) {
    let elementRef = `nameInputRef${i}`;
    setTimeout(() => {
        console.log(this);

        if (this[elementRef])
        this[elementRef].nativeElement.value = 'Apple';
    }, 100);
  }

Here is the demo - https://stackblitz.com/edit/angular-g3tzmh这是演示 - https://stackblitz.com/edit/angular-g3tzmh

Use multiple ViewChild to identify the inputs.使用多个 ViewChild 来识别输入。 And, then use the second argument to editAge() to know which input field needs to be rendered/updated.并且,然后使用 editAge() 的第二个参数来了解需要呈现/更新哪个输入字段。

Because initially, the input fields are not part of the DOM, we use setTimeout() to let the DOM be updated first, so that by the time we try to set the value of the corresponding input field, it will be available in the DOM.因为最初,输入字段不是 DOM 的一部分,所以我们使用 setTimeout() 让 DOM 先更新,这样当我们尝试设置相应输入字段的值时,它就会在 DOM 中可用.

app.component.html app.component.html

<table>
  <tbody>
    <tr>
      <td>John</td>
      <td>
        <select (change)="editAge(selectField1.value, 1)" #selectField1>
          <option value="1">Less than 10</option>
          <option value="2">Greater than 10 and Less than 80</option>
          <option value="3">Less than 80</option>
        </select>
      </td>
      <td>
        <span *ngIf="!(+selectField1.value == 2)">24</span>
        <span *ngIf="+selectField1.value == 2">
          <input type="text" #textField1 />
        </span>
      </td>
    </tr>
    <tr>
      <td>Jacky</td>
      <td>
        <select (change)="editAge(selectField2.value, 2)" #selectField2>
          <option value="1">Less than 10</option>
          <option value="2">Greater than 10 and Less than 80</option>
          <option value="3">Less than 80</option>
        </select>
      </td>
      <td>
        <span *ngIf="!(+selectField2.value == 2)">4</span>
        <span *ngIf="+selectField2.value == 2">
          <input type="text" #textField2 />
        </span>
      </td>
    </tr>
  </tbody>
</table>

app.component.ts应用程序组件.ts

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  @ViewChild('textField1') nameInputRef1: ElementRef;
  @ViewChild('textField2') nameInputRef2: ElementRef;
  editAge(ee, i) {
    if (+ee !== 2) return;
    setTimeout(() => {
      if (+i === 1) this.nameInputRef1.nativeElement.value = 'Apple';
      else this.nameInputRef2.nativeElement.value = 'Orange';
    }, 500);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM