简体   繁体   中英

Problem <mat-chip> on angular material design

My problem is: i have mat-chip list and if i close the first item it's ok and if i close the last item all are close:

在此处输入图片说明

this is my 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>

and this is my ts:

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

and if i use:

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

it's ok but my data are not update

and this is my reducer code:

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) });

From your comment you are doing this:

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

whereas you should do this:

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

You want to use the array that has been spliced, not the array returned from the splice.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM