简体   繁体   English

Angular - 如何通过一种方式绑定在 ngFor 循环中获取输入值

[英]Angular - How to get input value at ngFor loop with one way binding

Can you give me a way to get input value at ngFor loop with one way binding?你能给我一种方法来通过单向绑定在 ngFor 循环中获取输入值吗?

<div *ngFor="let d of dataList">
  <input #inputValue type="text" [ngValue]="d.value">
  <button *ngIf="!d.open" (click)="d.open = true">change</button>
  <button *ngIf="d.open" (click)="save(d.id, NEWVALUE); d.open = false;">save</button>
  <button *ngIf="d.open" (click)="d.open = false">cancel</button>
</div>`

How can I set NEWVALUE?如何设置 NEWVALUE? with two-way binding is easy.用双向绑定很容易。 but after click cancel, value already changed as I don't want.但单击取消后,值已更改,因为我不想要。 So would avoid that way.所以会避免这种方式。

One solution I've found is using (ngModelChange).我发现的一种解决方案是使用 (ngModelChange)。

<div *ngFor="let d of dataList">
  <input #inputValue type="text" [ngValue]="d.value" (ngModelChange)="dataChanged($event)">
  <button *ngIf="!d.open" (click)="d.open = true">change</button>
  <button *ngIf="d.open" (click)="save(d.id); d.open = false;">save</button>
  <button *ngIf="d.open" (click)="d.open = false">cancel</button>
</div>


private newVal;
dataChanged(val) {
  this.newVal = val;
}
save(id) {
  saveDb(id, this.newVal);
}

This is not clear and optimized code as I guess.我猜这不是清晰和优化的代码。

As I know, template binding with # is also not work with ngFor.据我所知,使用# 进行模板绑定也不适用于 ngFor。 like喜欢

<div *ngFor="let d of dataList">
  <input #inputValue_{{d.id}} type="text" [ngValue]="d.value">
  <button *ngIf="d.open" (click)="save(inputValue_{{d.id}}.value); d.open = false;">save</button>
</div>

Do you have any good solution for me?你有什么好的解决办法吗?

It is not posible you must provide the template variable directly, but I did an alternative for you您必须直接提供模板变量是不可能的,但我为您做了一个替代方案

HTML HTML

<div *ngFor="let item of array">
  <input id="id_{{item.id}}" />
  <button type="button" (click)="printValue('id_'+item.id)"> buton {{item.id}}   </button>
</div>

Component成分

export class AppComponent  {
  array = [{id: 1}, {id: 2},{id: 3}]

  printValue(value: any){
    console.log(value);
    var containputiner = document.querySelector("#"+value);
    console.log(containputiner.value);
  }
}

Stackblitz Demo Stackblitz 演示

You can duplicate the array and use the data from the original array to set the previous value as if it were in memory.您可以复制数组并使用原始数组中的数据来设置先前的值,就像它在内存中一样。

See my code example: https://stackblitz.com/edit/angular-a4eucy查看我的代码示例: https : //stackblitz.com/edit/angular-a4eucy

You can create a new component that will represent an item from the list.您可以创建一个新组件来表示列表中的项目。

app-item-list.ts app-item-list.ts

import { Component, Input, Ouput, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-item-list'
  templateUrl: './app-item-list.html'
})

export class ItemListComponent {

  newValue: string;
  @Input() data: YourDataClassType;
  @Output() onUpdate: EventEmitter<{id: number, newValue: string}>;

  contructor() {

    this.onUpdate = new EventEmitter<{id: number, newValue: string}>;
    this.newValue = data.value;
  }

  save() {

    this.onUpdate.emit({id: this.data.id, newValue: this.newValue});
  }
}

app-item-list.html app-item-list.html

<div>
  <input type="text" [(ngValue)]="newValue">
  <button *ngIf="!data.open" (click)="data.open = true">change</button>
  <button *ngIf="data.open" (click)="save(); d.open = false;">save</button>
  <button *ngIf="data.open" (click)="data.open = false">cancel</button>
</div>

parent-component.html父组件.html

<app-item-list *ngFor="let d of datalist" [data]="d" (onUpdate)="save($event)" />

parent-component.ts父组件.ts

save($event) {
  console.log($event.id, $event.newValue);
}

Remember to include 'app-item-list' in your module declaration.请记住在模块声明中包含“app-item-list”。

This code might need some refactor.此代码可能需要一些重构。 I made it without testing and I am not using angular for a while.我在没有测试的情况下制作了它,而且我有一段时间没有使用 angular。

home.component.html主页.component.html

<div>
<ul>
<li style="border:1px solid;width:25%;margin-bottom:1%;cursor:pointer;" *ngFor="let items of statusdata"  (click)="getData(items)"><span>{{items.id}}</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>{{items.name}}</span></li>
</ul>
<select  [(ngModel)]="getOption">
<option>2019-2020</option>
</select>
</div>

home.component.ts home.component.ts

import { Component, OnInit } from '@angular/core';
import { CommonserviceService } from './../utilities/services/commonservice.service';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
declare var $: any;
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  getIt:any;
  statusdata: any;
  getOption:any;
  ngOnInit() {

      this.statusdata = [{ id: 1, name: 'Angular 2+' },
    { id: 2, name: 'Angular 4' },
    { id: 3, name: 'Angular 5' },
    { id: 4, name: 'Angular 6' },
    { id: 5, name: 'Angular 7' }
  ];
console.log(this.getOption);

  }
  getData(items){
      console.log(items.name+"and"+items.id);

  } 

}

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

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