AngularMaterial自动完成:如何在选项选择时防止值更改?
创始人
2024-10-28 12:00:49
0

可以通过使用MatAutocompleteSelectedEvent来获取当前所选项的值,并将它作为输入框的初始值进行设置,以阻止值的更改。下面是代码示例:


  
  
    
      {{option}}
    
  

import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs';
import { startWith, map } from 'rxjs/operators';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';

@Component({
  selector: 'app-autocomplete',
  templateUrl: './autocomplete.component.html',
  styleUrls: ['./autocomplete.component.css']
})
export class AutocompleteComponent implements OnInit {
  myControl = new FormControl();
  options: string[] = ['Angular', 'React', 'Vue'];
  filteredOptions: Observable;

  constructor() { }

  ngOnInit(): void {
    this.filteredOptions = this.myControl.valueChanges.pipe(
      startWith(''),
      map(value => this._filter(value))
    );
  }

  private _filter(value: string): string[] {
    const filterValue = value.toLowerCase();

    return this.options.filter(option => option.toLowerCase().includes(filterValue));
  }

  onOptionSelected(event: MatAutocompleteSelectedEvent): void {
    this.myControl.setValue(event.option.value, {emitEvent: false});
  }
}

在这个示例中,选中一个选项时,它不会改变输入框的值,而是将该选项设置为输入框的初始值。值的更改会被阻止。

相关内容

热门资讯

玩家必看攻略!wejoker辅... 玩家必看攻略!wejoker辅助软件价格(辅助挂)原来存在有挂(有挂细节辅助方法)1、完成有辅助插件...
玩家必看攻略!wepoker透... 玩家必看攻略!wepoker透视脚本下载(辅助挂)都是是有挂(有挂头条辅助攻略)该软件可以轻松地帮助...
科普分享!wepoker好友局... 科普分享!wepoker好友局透视(辅助挂)总是是有挂(有挂辅助辅助插件)1、实时透视辅助更新:用户...
一分钟了解!wepokerh5... 一分钟了解!wepokerh5破解(辅助挂)本来存在有挂(有挂规律辅助方法)是不是有人用挂微扑克wp...
最新技巧!德普之星透视(辅助挂... 最新技巧!德普之星透视(辅助挂)竟然是有挂(真的有挂辅助方法)1、公共底牌简单,透视插件操作,插件下...
一分钟秒懂!wepoker透视... 一分钟秒懂!wepoker透视脚本免费下载(辅助挂)原来真的有挂(有挂分享辅助app)脚本下载中分为...
2分钟细说!aapoker怎么... 2分钟细说!aapoker怎么开辅助器(辅助挂)切实有挂(有挂秘诀辅助插件)暗藏猫腻,小编详细说明破...
必备教程!来玩德州破解器(辅助... 必备教程!来玩德州破解器(辅助挂)本来真的是有挂(有挂助手辅助插件)1)辅助插件:进一步探索辅助大陆...
重磅来袭!德扑HHpoker有... 重磅来袭!德扑HHpoker有挂吗(辅助挂)真是真的是有挂(有挂助手辅助教程)1、很好的工具软件,可...
关于!佛手在线大菠萝技巧(辅助... 关于!佛手在线大菠萝技巧(辅助挂)本来是真的有挂(存在有挂辅助软件)1、辅助软件下载优化,底牌透视脚...