【发布时间】:2018-08-22 12:51:47
【问题描述】:
我正在使用 Angular 6 应用程序,我正在尝试使用输入框进行多项选择没有任何第三方插件、jquery、datalist、选择框,它是纯输入框,基于打字稿。
HTML:
<div class="autocomplete">
<input name="suggestion" type="text" placeholder="User" (click)="suggest()" [formControl]="typeahead">
<div class="autocomplete-items" *ngIf="show">
<div *ngFor="let s of suggestions" (click)="selectSuggestion(s)">{{ s }}</div>
</div>
</div>
TS:
import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
suggestions: string [] = [];
suggestion: string;
show: boolean;
typeahead: FormControl = new FormControl();
fieldHistory: string [] = [];
suggest() {
this.suggestions = this.users;
this.show = true;
}
selectSuggestion(s) {
this.suggestion = "";
this.fieldHistory.push(s)
for (let i = 0; i < this.fieldHistory.length; i++)
this.suggestion = this.suggestion + " " + this.fieldHistory[i];
this.typeahead.patchValue(this.suggestion);
this.show = false;
}
users = ['First User', 'Second User', 'Third User', 'Fourth User'];
}
这里我需要删除选择的值,如角材料芯片,用户可以选择多个值,但他也可以删除错误选择的值。
如何为每个单独的项目设置删除选项以删除输入框中错误选择的值?
Stackblitz链接与多选选项https://stackblitz.com/edit/angular-dndhgv
在上述链接中进行任何编辑以使用删除选项进行多选也将更加明显..
【问题讨论】:
标签: javascript html angular typescript multi-select