如何在Angular中传递参数

问题描述 投票:0回答:1

我正在使用mat-autocomplete,由于某些原因,当我从下拉列表中选择项目时,我的函数无法正常工作,因此我认为我需要在select中传递add()函数,但我得到的是“预期的1个参数但为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>
html angular typescript angular-material angular-directive
1个回答
0
投票

您需要将事件传递给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);
  }
© www.soinside.com 2019 - 2024. All rights reserved.