当前位置: 首页 > 知识库问答 >
问题:

MAT-自动完成未正确绑定

通鸿风
2023-03-14

我尝试使用两个matInput字段,每个字段都绑定到单独的mat-autocomplete面板。按照这里的步骤,我可以让一个工作正常,但我有困难与两个输入字段和自动完成面板。

<form>

  <mat-form-field>
    <input matInput [matAutocomplete]="first" [formControl]="myControl">
     <mat-autocomplete #first="matAutocomplete">
       <mat-option *ngFor="let option of filteredOptions1 | async" [value]="option">
          {{option}}
        </mat-option>
     </mat-autocomplete>
   </mat-form-field>


   <mat-form-field>
     <input matInput [matAutocomplete]="second" [formControl]="otherControl">
     <mat-autocomplete #second="matAutocomplete">
       <mat-option *ngFor="let option of filteredOptions2 | async" [value]="option">
          {{option}}
       </mat-option>
      </mat-autocomplete>
   </mat-form-field>

</form>
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';


@Component({
  selector: 'app-property-create',
  templateUrl: './property-create.component.html',
  styleUrls: ['./property-create.component.css']
})

export class PropertyCreateComponent implements OnInit {

  myControl = new FormControl();
  otherControl = new FormControl();

  options1: string[] = ['One', 'Two', 'Three'];
  options2: string[] = ['Four', 'Five', 'Six'];

  filteredOptions1: Observable<string[]>;
  filteredOptions2: Observable<string[]>;

  constructor() { }

  ngOnInit() {

    this.filteredOptions1 = this.myControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this._filter(value))
      );

    this.filteredOptions2 = this.otherControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this._filter2(value))
      );
  }

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

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

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

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

}

有人看到这个或者知道我做错了什么吗?

共有1个答案

程举
2023-03-14

结果是您缺少了form和mat-form-field元素的css类:

<form class="example-form">
  <mat-form-field class="example-full-width">
    <input  placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="first">
    <mat-autocomplete #first="matAutocomplete">
      <mat-option *ngFor="let option of filteredOptions1|async" [value]="option">
        {{option}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
  <mat-form-field class="example-full-width">
    <input placeholder="Pick one" aria-label="Number" matInput [formControl]="otherControl" [matAutocomplete]="second">
    <mat-autocomplete #second="matAutocomplete">
      <mat-option *ngFor="let option of filteredOptions2| async" [value]="option">
        {{option}}
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
</form>

Stackblitz:https://Stackblitz.com/edit/angular-gqax9d

 类似资料:
  • 代码如下: 我从文件中了解到: 如果您使用的是@SpringBootTest注释,则TestRestTemplate是自动可用的,并且可以@AutoWired到您的测试中。 问题是我确实使用了SpringBootTest注释,但当我运行测试时,TestRestTemplate总是为空。也许我错过了什么。 编辑:我在添加@RunWith(SpringRunner.class)注释时遇到了完全相同的问

  • 问题内容: 如何使用Redis实现自动完成功能? 比如说我有一个数组。当我型我得到 我希望你明白这一点。我如何有效地使用redis命令来实现这一点(如果可能,但我认为是)。如果我能通过telnet尝试一些简单的命令来模仿这种行为,那就太好了。 谢谢 问题答案: 如果您要处理的是大型数据集,建议您考虑将其实现。我将一小部分Ruby做到了这一点: 例如: 在Wikipedia的Tries条目上阅读有关

  • 自动完成是现代网站中经常使用的一种机制,用于向用户提供他/她在文本框中键入的单词开头的建议列表。 然后,用户可以从列表中选择一个项目,该项目将显示在输入字段中。 此功能可防止用户输入整个单词或一组单词。 JQueryUI提供了一个自动完成窗口小部件 - 一个与下拉列表非常相似的控件,但过滤选项只显示与用户在控件中键入的内容相匹配的选项。 jQueryUI提供了autocomplete()方法,用于

  • md-autocomplete是一个Angular Directive,用作一个特殊的输入控件,带有内置下拉列表,显示与自定义查询的所有可能匹配。 一旦用户键入输入区域,该控件就充当实时建议框。 《md-autocomplete》可用于从本地或远程数据源提供搜索结果。 执行查询时md-autocomplete缓存结果。 第一次调用后,它使用缓存的结果来消除不必要的服务器请求或查找逻辑,并且可以禁用

  • 描述 (Description) 自动填充是Framework7的移动友好和触摸优化组件,可以是下拉列表或独立方式。 您可以使用JavaScript方法创建和初始化自动完成实例 - myApp.autocomplete(parameters) 其中parameters是用于初始化自动完成实例的必需对象。 自动填充参数 下表列出了Framework7中可用的自动填充参数 - S.No 参数和描述

  • 我正在编写一个应用程序,通过WebSocket从Netty服务器向客户端发送小文件(~2kb)。 为了测试文件是否发送成功,我进行了以下测试。 客户端连接到服务器 设置为从客户端计算机上的服务器上丢弃所有数据包 服务器向客户端发送一个文件 正在服务器上检查“ChannelFuture”的结果 当我在这个测试中发送一个2kb的文件时,我立即从“future.isSuccess()”和“future.