gpt4 book ai didi

typescript - 如何在角材2中使用垫片和自动完成功能保存所选对象

转载 作者:行者123 更新时间:2023-12-03 18:26:18 25 4
gpt4 key购买 nike

我正在将Angular 6与Angular Material一起使用。我正在尝试从mat-chip和自动完成功能中保存选定的对象或选定对象的列表。我能够将字符串值发送到fruits []数组,但是无法将所选对象发送到fruits []数组。请帮助我找到解决方案。谢谢。

我的演示项目链接:demo code on stackblitz

最佳答案

您可以尝试此解决方案。

我已经在Stackblitz上创建了一个演示。

component.html


<mat-form-field class="example-chip-list">
<mat-chip-list #chipList>
<mat-chip *ngFor="let fruit of fruits;let indx=index;" [selectable]="selectable" [removable]="removable" (removed)="remove(fruit,indx)">
{{fruit.name}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input placeholder="New fruit..." #fruitInput [formControl]="fruitCtrl" [matAutocomplete]="auto" [matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="addOnBlur" (matChipInputTokenEnd)="add($event)">
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit.name}}
</mat-option>
</mat-autocomplete>
</mat-form-field>


<pre>{{fruits|json}}</pre>

component.ts


import { COMMA, ENTER } from '@angular/cdk/keycodes';
import { Component, ElementRef, ViewChild } from '@angular/core';
import { FormControl } from '@angular/forms';
import { MatAutocompleteSelectedEvent, MatChipInputEvent } from '@angular/material';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';

/**
* @title Basic chips
*/
@Component({
selector: 'chips-overview-example',
templateUrl: 'chips-overview-example.html',
styleUrls: ['chips-overview-example.css'],
})
export class ChipsOverviewExample {
visible = true;
selectable = true;
removable = true;
addOnBlur = false;
separatorKeysCodes: number[] = [ENTER, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: any = [];
allFruits: any = [
{
id: 1,
name: 'Apple'
},
{
id: 2,
name: 'Orange'
},
{
id: 3,
name: 'Banana'
},
{
id: 4,
name: 'Malta'
}
];

@ViewChild('fruitInput') fruitInput: ElementRef;

constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
}

add(event: MatChipInputEvent): void {
const input = event.input;
const value = event.value;
// Add our fruit
if ((value || '').trim()) {
this.fruits.push({
id:Math.random(),
name:value.trim()
});
}

// Reset the input value
if (input) {
input.value = '';
}

this.fruitCtrl.setValue(null);
}

remove(fruit, indx): void {
this.fruits.splice(indx, 1);
}

selected(event: MatAutocompleteSelectedEvent): void {
this.fruits.push(event.option.value);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}

private _filter(value: any): string[] {
return this.allFruits.filter(fruit => fruit.id === value.id);
}
}

关于typescript - 如何在角材2中使用垫片和自动完成功能保存所选对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com