【问题标题】:How to update object value in javascript如何在javascript中更新对象值
【发布时间】:2021-06-12 01:22:35
【问题描述】:

尝试更新对象值但不起作用。是否可以更新对象值。如何找到解决方案。如果有人知道请帮忙。

updateFn() {
var elements: any = document.getElementsByTagName("input");
for (var i = 0; i < elements.length; i++) {
  if (elements[i].type == "text") {
    this.selVal[i].key = elements[i].value;
    console.log(elements[i].value);
  }
}
}

演示:https://stackblitz.com/edit/angular-gss26x?file=src%2Fapp%2Fapp.component.ts

【问题讨论】:

    标签: javascript typescript angular8


    【解决方案1】:

    我认为这是您尝试做的,这是stackblitz 上的示例

    在 HTML 上:

    {{selValues | json}}
    <form #form="ngForm">
      <table class="auto" width="100%">
        <thead>
          <tr>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let item of selValues | keyvalue">
            <td>{{item.key}}</td>
            <td><input type="text" [name]="item.key" [ngModel]="item.value"></td>
          </tr>
        </tbody>
      </table>
    </form>
    
    <button (click)="resetFn()">Reset</button>
    <button (click)="updateFn(form)">Update</button>

    在ts文件上:

      selValues: any;
    
      originalValues: any = {
        name: "Test",
        gender: "Male",
        id: 1
      };
    
      ngOnInit() {
        this.resetFn();
      }
    
      resetFn() {
        this.selValues = { ...this.originalValues };
      }
    
      updateFn(form: NgForm) {
        this.selValues = form.value;
      }
    }

    ** 因为您使用的是keyvalue 管道,所以您不能使用ngModel 作为双向绑定。检查this link

    【讨论】:

    猜你喜欢
    • 2016-03-05
    • 1970-01-01
    • 2021-07-17
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2019-05-02
    • 1970-01-01
    相关资源
    最近更新 更多