Html 如何在角坐标中传递参数
我正在使用mat autocomplete,由于某种原因,当我从下拉列表中选择项时,我的函数不起作用,因此我认为我需要在select中传递add()函数,但我得到的结果是“预期1个参数,但得到0”。我真的不知道该通过什么 任何建议Html 如何在角坐标中传递参数,html,angular,typescript,angular-material,angular-directive,Html,Angular,Typescript,Angular Material,Angular Directive,我正在使用mat autocomplete,由于某种原因,当我从下拉列表中选择项时,我的函数不起作用,因此我认为我需要在select中传递add()函数,但我得到的结果是“预期1个参数,但得到0”。我真的不知道该通过什么 任何建议 add(event: MatChipInputEvent): void { const input = event.input; const value = event.value; this.tagService.addTag(this.
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
选择一个超级标记
{{superTag.tag}
取消
{{tag}}
您需要将事件传递给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函数:
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);
}
add函数需要MatChipInputEvent事件作为参数。您需要向它传递这样的事件
this.add(event)
您可能应该删除divs封装,最终可能会得到类似的结果。
`
选择一个超级标记
{{superTag.tag}
取消
{{tag}}
`请提供HTML代码,以便更好地understand@OAH嗨,我刚上传了HTML。。谢谢你,还有一个问题。所以在下拉列表中,我有两个词MAIX和Chart(后端数据)。当任何一个用户从下拉列表中选择时,我都是未定义的,所以你们有机会知道为什么会发生吗?。当用户键入其中一个并按enter键时,工作正常,但当我从下拉列表中选择时,我没有定义。可能您的类型错误,请尝试此操作,而不是
selected(event){}
,在访问事件之前,请尝试执行控制台console.log('my event',event)
,以确保获得预期的数据
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);
}
<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>