【问题标题】:How to achieve 2 way data binding between html table and angular 2 component如何实现 html 表格和 Angular 2 组件之间的 2 路数据绑定
【发布时间】:2017-03-01 18:48:21
【问题描述】:

我有一个使用 *ngFor 指令填充的表。用户可以单击表格中的每个单元格并通过“contentEditable”属性编辑其值。当前实现会更新表中的数据,但不会更新组件中的数据。当用户使用 Angular 2 原生的工具更改单元格中的数据时,是否可以更新组件中的数据?如果有,怎么做?

这是html文件:

<table id="data-table" class="table table-striped">
    <thead>
      <tr>
        <th *ngFor="let round of rounds">{{ round.title }}</th>
      </tr>
    </thead>
    <tbody>
      <tr *ngFor="let player of players">
        <td contenteditable="true">{{ player.name }}</td>
        <td contenteditable="true">{{ player.round1 }}</td>
        <td contenteditable="true">{{ player.round2 }}</td>
        <td contenteditable="true">{{ player.round3 }}</td>
      </tr>
    </tbody>
  </table>

这是组件:

import { Component, OnInit } from '@angular/core';
import { BooksAndRunService } from '../books_and_run.service';



@Component({
  moduleId: module.id,
  selector: 'books-and-run-play',
  templateUrl: './books_and_run_play.component.html',
  styleUrls: ['./books_and_run_play.component.css'],
})


export class BooksAndRunPlayComponent implements OnInit {
  constructor(private booksAndRunService: BooksAndRunService) { }
  currentRound = 1;

  rounds = [
    {title: ""},
    {title: "Round 1"},
    {title: "Round 2"},
    {title: "Round 3"},
    {title: "Round 4"},
    {title: "Round 5"},
    {title: "Round 6"},
    {title: "Round 7"},
  ]

  players = [
    {
      name: "Bob",
      round1: 0,
      round2: 0,
      round3: 0,
      round4: 0,
      round5: 0,
      round6: 0,
      round7: 0,
    },
    {
      name: "Joe",
      round1: 0,
      round2: 0,
      round3: 0,
      round4: 0,
      round5: 0,
      round6: 0,
      round7: 0,
    },
    {
      name: "Jim",
      round1: 0,
      round2: 0,
      round3: 0,
      round4: 0,
      round5: 0,
      round6: 0,
      round7: 0,
    },
    {
      name: "Sarah",
      round1: 0,
      round2: 0,
      round3: 0,
      round4: 0,
      round5: 0,
      round6: 0,
      round7: 0,
    },
    {
      name: "Jane",
      round1: 0,
      round2: 0,
      round3: 0,
      round4: 0,
      round5: 0,
      round6: 0,
      round7: 0,
    },
    {
      name: "Jill",
      round1: 0,
      round2: 0,
      round3: 0,
      round4: 0,
      round5: 0,
      round6: 0,
      round7: 0,
    },
  ];



  ngOnInit(): void {
    // this.players = this.booksAndRunService.getPlayers();

  }

}

【问题讨论】:

    标签: javascript html angular


    【解决方案1】:

    使用 contenteditable="true" 时,您不会获得 ngModel 属性,因此您需要创建自己的 2 路数据绑定:

    <td contenteditable="true" [textContent]="player.name"   (input)="player.name = $event.target.textContent"></td>
    

    您也可以这样做

    <td contenteditable="true" [textContent]="player.name"   (input)="onContentChanged($event)"></td>
    

    然后在你的组件类中

    onContentChanged(event : Event)
        {
            let playerName : string = event.target.textContent;
    
            .... Do stuff here ....
        }
    

    【讨论】:

    • 太棒了,谢谢!只是好奇,这是在表中的每个单元格上创建一个事件侦听器吗?
    • 是的,如果您想要 2 路数据绑定,您必须为每个使用 contenteditable="true" 的元素执行此操作。所以是的,当 ngFor 完成时,您最终会为每个元素创建一个事件侦听器,但这应该不是问题。请注意,不要弄错您正在调用的对象的哪些属性,仅此而已:)
    猜你喜欢
    • 1970-01-01
    • 2016-04-18
    • 2017-06-05
    • 2017-11-05
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    相关资源
    最近更新 更多