如何在 Angular 中传递参数

How to pass argument in Angular

我正在使用 mat-autocomplete 并且由于某些原因我的功能在我 select 下拉列表中的项目时无法正常工作所以我想我需要在我的 [=21] 中传递 add() 函数=] 但我知道它是 "expected 1 argument but got 0"。我不太确定要传递什么。

任何建议,

  add(event: MatChipInputEvent): void {
    const input = event.input;
    const value = event.value;
    this.tagService.addTag(this.workspace.guid, 'workspace', value).subscribe((tag) => console.log("added", tag));
    if ((value || '').trim()) {
      this.superTags.push({tag: value.trim(), type: TagType.super});
    }
    if (input) {
      input.value = '';
    }
    this.tagCtrl.setValue(null);
  }

 selected(event: MatAutocompleteSelectedEvent): void {
    const tag = {tag: event.option.viewValue, type: TagType.super};
    this.superTags.push(tag);
    this.add()             //I'm getting error that it expected 1 argument

    if(this.input){
      this.input.nativeElement.value = "";
    }
    this.tagCtrl.setValue(null);
    var index = this.allSuperTagNames.indexOf(tag.tag);
    this.allSuperTagNames.splice(index, 1);
    this.mapper();
  }

HTML

    <mat-form-field class="form" appearance="fill">
        <mat-label>Select a Super Tag</mat-label>
        <mat-chip-list #chipList>
            <div>
                <mat-chip *ngFor="let superTag of superTags" [selectable]="selectable" [removable]="removable"
                    (removed)="remove(superTag)">
                    {{superTag.tag}}
                    <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
                </mat-chip>
            </div>
            <div>
                <input matInput #input [formControl]="tagCtrl" [matAutocomplete]="auto" [matChipInputFor]="chipList"
                    [matChipInputSeparatorKeyCodes]="separatorKeysCodes" (matChipInputTokenEnd)="add($event)">
            </div>
        </mat-chip-list>
        <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
            <mat-option *ngFor="let tag of filteredSuperTags | async" [value]="tag">
                {{tag}}
            </mat-option>
        </mat-autocomplete>
    </mat-form-field>

您需要将事件传递给 add() 函数:

 selected(event: MatAutocompleteSelectedEvent): void {
    const tag = {tag: event.option.viewValue, type: TagType.super};
    this.superTags.push(tag);
    this.add(event)             //pass the event 

    if(this.input){
      this.input.nativeElement.value = "";
    }
    this.tagCtrl.setValue(null);
    var index = this.allSuperTagNames.indexOf(tag.tag);
    this.allSuperTagNames.splice(index, 1);
    this.mapper();
  }

检查像这样声明添加函数:

 add(event){
    const input = event.input;
    const value = event.value;
    this.tagService.addTag(this.workspace.guid, 'workspace', value).subscribe((tag) => console.log("added", tag));
    if ((value || '').trim()) {
      this.superTags.push({tag: value.trim(), type: TagType.super});
    }
    if (input) {
      input.value = '';
    }
    this.tagCtrl.setValue(null);
  }

添加函数需要一个 MatChipInputEvent 事件作为参数。您需要向它传递一个像这样的事件 this.add(event)

你应该去掉封装 and 的 div,你可能会得到这样的结果。 `

<mat-form-field class="form" appearance="fill">
  <mat-label>Select a Super Tag</mat-label>
  <mat-chip-list #chipList>
    <mat-chip *ngFor="let superTag of superTags" [selectable]="selectable" [removable]="removable"
        (removed)="remove(superTag)">
        {{superTag.tag}}
        <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>
    <input matInput #input [formControl]="tagCtrl" [matAutocomplete]="auto" [matChipInputFor]="chipList"
        [matChipInputSeparatorKeyCodes]="separatorKeysCodes" (matChipInputTokenEnd)="add($event)">
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
      <mat-option *ngFor="let tag of filteredSuperTags | async" [value]="tag">
          {{tag}}
      </mat-option>
  </mat-autocomplete>
</mat-form-field>

`