【问题标题】:Aurelia repeat.for within repeat.forAurelia repeat.for 在 repeat.for 内
【发布时间】:2020-01-03 19:33:14
【问题描述】:

我会尽量保持简单。

我们有一个电台列表,每个电台最多可以设置 4 个频道。

您可以更改每个站点的 4 个站点中的任何一个。 表格下方还有一个汇总表,显示了所选择的内容。

对下面汇总表中的站级更新所做的任何更改,但对频道的任何更新都不会。我想知道这是否与这里的答案有关https://stackoverflow.com/a/42629584/9431766

我很困惑的是,如果电台显示更新,但频道没有更新。

这里是简化版的代码

constructor() {
  this.stations = [
    {
      name: 'Station 1',
      channels: [null, null, null, null]
    },
    {
      name: 'Station 2',
      channels: [null, null, null, null]
    },
    {
      name: 'Station 3',
      channels: [null, null, null, null]
    }
  ];

  this.channels = [
    { id: 1, name: 'Channel 1' },
    { id: 2, name: 'Channel 2' },
    { id: 3, name: 'Channel 3' },
    { id: 4, name: 'Channel 4' },
  ];

  this.activeStation = {}
}

editStation(station) {
  this.activeStation = station;
}
<div class="station">
  <input value.bind="activeStation.name"/>
  <div repeat.for="select of activeStation.channels">
    <select value.bind="activeStation.channel[$index]">
      <option model.bind="item" repeat.for="item of channels"></option>
    </select>
  </div>
</div>

<div class="summary">
  <div repeat.form="station of stations">
    <h3>${station.name}</h3>
    <div repeat.for="channel of station.channels">
      ${channel ? channel.name : 'N/A'}
    </div>
    <div class="edit"><a click.delegate="editStation(station)">Edit</a></div>
  </div>
</div>

如果我在每个电台更新后重置频道,那么摘要才会更新。 我通过使用地图重新映射车站来做到这一点,即; this.activeStation.channels = this.activeStation.channels.map(station);

我宁愿不必在每次更新后重置频道,这似乎有点矫枉过正。

【问题讨论】:

    标签: aurelia


    【解决方案1】:

    好问题。您观察到的行为是因为 &lt;div/&gt; 内部 div.summary 元素中的 repeat 看不到数组的更改,因为突变是通过索引设置器完成的(由 &lt;select/&gt; 绑定引起的)。所以我们有两个选择:

    1. 将每个station 的数组更改为channels 通知数组观察者
    2. 让重复知道数组内部的变化。

    对于 (2),我们可以按照您的方式进行,也可以使用值转换器的方式来避免在编辑时修改源数组。你可以在这里看到一个例子https://codesandbox.io/s/httpsstackoverflowcomquestions59571360aurelia-repeat-for-within-repeat-for-yuwwv

    对于 (1),我们需要通过 change 事件解决 select 的手动更改处理

    编辑:在 v2 中,我们已经修复了这个问题,因此它可以正常且自然地工作,而无需我们添加这些工作。

    【讨论】:

    • 嘿@bigopon,谢谢你的回复。我会查看您的解决方案,看看它是否比我目前使用的解决方案更有效,然后等待 v2 出现,无论如何我们都会在它出现时移植到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2021-05-05
    相关资源
    最近更新 更多