首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何避免使用angular13将更新后的值绑定到其他数组

如何避免使用angular13将更新后的值绑定到其他数组
EN

Stack Overflow用户
提问于 2022-06-14 11:10:04
回答 1查看 30关注 0票数 0

我正在做angular13,这里有一个按钮,可以从左向右移动项目。现在,一旦项目从左向右移动,如果我在右侧编辑相同的项,则编辑后的值也会在左侧数组中更新。

HTML:

代码语言:javascript
复制
<div class="card">
  <div class="card-header" id="headingBasicAgentsGroupView">
    <a
      href="javascript:void(0);"
      class="d-block h5 mb-0"
      data-target="#basicAgentsGroupView"
      aria-expanded="false"
      aria-controls="basicAgentsGroupView"
      >Group View</a
    >
  </div>
  <div
    class=""
    id="basicAgentsGroupView"
    aria-labelledby="headingBasicAgentsGroupView"
    data-parent="#basicTabAccordion"
  >
    <div class="card-body" id="agents-group-view">
      <div class="row">
        <div class="col">
          <div class="form-group">
            <h6 class="font-weight-bold">Group</h6>
          </div>
        </div>
        <div class="col-2"></div>
        <div class="col">
          <div class="form-group">
            <h6 class="font-weight-bold">Not in Group</h6>
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col">
          <div class="card shadow-sm border-0 swap-list list-left">
            <div class="card-header border-bottom-0 rounded-top">
              <div class="row">
                <div class="col d-flex align-items-center">
                  <input
                    type="text"
                    name="SearchDualList"
                    class="form-control"
                    placeholder="Search"
                  />
                </div>
              </div>
            </div>
            <div class="card-body overflow-auto py-0">
              <ul
                class="list-group list-group-flush"
                *ngFor="let child of agentInView"
                name="agentInGroup"
              >
                <li
                  class="list-group-item {{
                    isInActiveGroupItems(child) ? 'active' : ''
                  }}"
                  (click)="toggleActiveInGroup(child)"
                >
                  {{ child.value }}
                </li>
              </ul>
            </div>
          </div>
        </div>
        <div
          class="col-2 d-flex flex-column justify-content-center list-arrows"
        >
          <button
            type="button"
            class="btn btn-outline-primary my-3 move-right"
            (click)="moveToTheRight()"
          >
            Move Right
          </button>
          <button
            type="button"
            class="btn btn-outline-primary my-3 move-left"
            (click)="moveToTheLeft()"
          >
            Move Left
          </button>
        </div>
        <div class="col">
          <div class="card shadow-sm border-0 swap-list list-right">
            <div class="card-header border-bottom-0 rounded-top">
              <div class="row">
                <div class="col d-flex align-items-center">
                  <input
                    type="text"
                    class="form-control"
                    placeholder="Search"
                  />
                </div>
              </div>
            </div>
            <div class="card-body overflow-auto py-0">
              <ul
                class="list-group list-group-flush"
                *ngFor="let child of agentNotinView"
                name="agentNotInGroup"
              >
                <li
                  class="list-group-item {{
                    isInActiveNotGroupItems(child) ? 'active' : ''
                  }}"
                  (click)="toggleActiveNotInGroup(child)"
                >
                  <input type="text" [(ngModel)]="child.value" />
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

TS:

代码语言:javascript
复制
public moveToTheRight(): void {
    this.move('agentInView', 'agentNotinView');
  }

  public moveToTheLeft(): void {
    this.move('agentNotinView', 'agentInView');
  }

  // moving function
  public move(from: string, to: string): void {
    this[from].filter((item: any, i: number) => {
      if (this.isInActiveGroupItems(item)) {
        this[to].push(item);
        this[from].push(item);
        return false;
      } else {
        return true;
      }
    });

    // if needed
    // sort if needed
    this[to].sort((a, b) => (a.id > b.id ? 1 : -1));

    // clean active items after action
    this.activeItemsGroupView.length = 0;
    //this.activeItemsNotInGroup.length = 0;
  }

  public toggleActiveInGroup(eventItem: any): void {
    if (this.activeItemsGroupView.find((item) => item === eventItem)) {
      this.activeItemsGroupView = this.activeItemsGroupView.filter(
        (item) => item !== eventItem
      );
    } else {
      this.activeItemsGroupView.push(eventItem);
    }
  }
  public toggleActiveNotInGroup(eventItem: any): void {
    if (this.activeItemsNotInGroup.find((item) => item === eventItem)) {
      this.activeItemsNotInGroup = this.activeItemsNotInGroup.filter(
        (item) => item !== eventItem
      );
    } else {
      this.activeItemsNotInGroup.push(eventItem);
    }
  }

  public isInActiveGroupItems(eventItem: any): boolean {
    return !!this.activeItemsGroupView.find((item) => item === eventItem);
  }
  public isInActiveNotGroupItems(eventItem: any): boolean {
    return !!this.activeItemsNotInGroup.find((item) => item === eventItem);
  }

演示

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-14 11:17:48

您正在将同一个实例推入两个数组中。您应该在某个时候创建一个新的对象,这样您就不会编辑相同的对象引用,并将您的更改传播到您不想要的地方。我建议在移动方法中使用析构方法来进行新的引用:

代码语言:javascript
复制
  // moving function
  public move(from: string, to: string): void {
    this[from].filter((item: any, i: number) => {
      if (this.isInActiveGroupItems(item)) {
        this[to].push({...item});
        this[from].push(item);
        return false;
      } else {
        return true;
      }
    });

    // if needed
    // sort if needed
    this[to].sort((a, b) => (a.id > b.id ? 1 : -1));

    // clean active items after action
    this.activeItemsGroupView.length = 0;
    //this.activeItemsNotInGroup.length = 0;
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72615941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档