棱角材料设计上的问题<mat-chip>

时间:2018-08-30 08:15:27

标签: javascript typescript angular-material

我的问题是:我有芯片清单,如果我关闭第一个项目就可以了,如果我关闭最后一个项目,则都可以了:

enter image description here

这是我的html:

<mat-form-field>
  <mat-chip-list #chipList>
    <mat-chip *ngFor="let keyword of keywords" [removable]="removable" (removed)="remove(keyword)">
      {{ keyword }}
      <mat-icon matChipRemove>cancel</mat-icon>
    </mat-chip>
    <input placeholder="{{ 'consultantSearchPage.searchForConsultantOrSkills' | translate }}" [matChipInputFor]="chipList" [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      (matChipInputTokenEnd)="addSearch($event)">
  </mat-chip-list>
</mat-form-field>

这是我的ts:

remove(keyword): void {
  const index = this.keywords.indexOf(keyword);
  if (index >= 0) {
    this._store.dispatch({ type: UPDATE_KEYWORDS, payload: index});
  }
}

如果我使用:

remove(keyword): void {
  const index = this.keywords.indexOf(keyword);
  if (index >= 0) {
    this.keywords.splice(index, 1);
  }
}

可以,但是我的数据没有更新

这是我的减速器代码:

export const UPDATE_KEYWORDS = 'UPDATE_KEYWORDS';
.......
case UPDATE_KEYWORDS:
  console.log( state.keywords.splice(0, 1));
  return Object.assign({}, state, { keywords: state.keywords.splice(action.payload, 1) });

1 个答案:

答案 0 :(得分:0)

根据您的评论,您正在这样做:

case UPDATE_KEYWORDS:
  console.log( state.keywords.splice(0, 1));
  return Object.assign({}, state, { keywords: state.keywords.splice(action.payload, 1) });

您应该这样做:

case UPDATE_KEYWORDS:
  state.keywords.splice(action.payload, 1);
  console.log(state.keywords);
  return Object.assign({}, state, { keywords: state.keywords });

您要使用已拼接的数组,而不是从拼接返回的数组。

https://www.w3schools.com/jsref/jsref_splice.asp