简体   繁体   English

Material.Angular.io mat-autocomplete [displayWith] function 更新 scope 变量

[英]Material.Angular.io mat-autocomplete [displayWith] function update scope variables

I'm running into an issue where I can access locally declared variables in the component controller instantiating the mat-autocomplete.我遇到了一个问题,我可以访问组件 controller 中本地声明的变量来实例化 mat-autocomplete。 The problem I'm facing is the local variables are stuck in this scope and I can't update them.我面临的问题是局部变量卡在这个 scope 中,我无法更新它们。

Any ideas or thoughts on updating the mat-autocomplete scope variables.关于更新 mat-autocomplete scope 变量的任何想法或想法。

Ultimately what I'm doing is concatenating the display string and a variable bound to the input model. This is giving me an autocomplete input that adds helper text for the user, ideally the text is up to date with clearing the input.最后,我正在做的是连接显示字符串和一个绑定到输入 model 的变量。这为我提供了一个自动完成输入,为用户添加了帮助文本,理想情况下文本是最新的并清除了输入。 The text is currently continuously concatenating, creating unusable text pretty quickly文本目前正在连续连接,很快就会创建无法使用的文本

html html

  <input
   [(ngModel)]="filter>

  mat-autocomplete
    #auto="matAutocomplete" 
    [displayWith]="displayFn">
    <mat-option
      *ngFor="let option of filteredOptions | async"
      [value]="option">
      {{ option }}
    </mat-option>
  </mat-autocomplete>

component.ts组件.ts

  displayFn(search): string | undefined {
    if(!search) return; //check if the search isn't already populated
    if(!search.match(/(=|\*)/)){
      if(this.filter){
        this.filter += ' ' + search + '==*term*';
      }else{
        this.filter = search +'==*term*';

      }
      return this.filter; //this isn't persisting across the lifecycle
    }
  }

You have two options, the first one is just calling [displayWith]="displayFn.bind(this)" which looks awful but I can confirm that this works (although I got an Error on my WebStorm saying "ng: Unknow Method bind") And the second one is to use an arrow function in order to preserve the context. 您有两个选择,第一个选择只是调用[displayWith]="displayFn.bind(this)" ,这看起来很糟糕,但是我可以确认这是可行的(尽管我在WebStorm上看到一个错误,说“ ng:未知方法绑定” )第二个是使用箭头功能以保留上下文。 Something like this: 像这样:

displayFn(offer?: Offer): string | undefined {
    return offer && offer.name == this.currentOffer.name ? offer.name : undefined;
}

displayFnWrapper() {
   return (offer) => this.displayFn(offer);
}

And in the template: 并在模板中:

<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFnWrapper()" (optionSelected)='assign($event.option.value)'>
    <mat-option *ngFor="let offer of filteredOffers$ | async" [value]="offer">{{ offer.name }}</mat-option>
</mat-autocomplete>

If I use an example, MyClass, where如果我使用示例 MyClass,其中

@Input() modeCity = false; @Input() modeCity = false;

in ngOnInit() I can access the modeCity and change it.在 ngOnInit() 中,我可以访问 modeCity 并更改它。 It is reflected over other methods in the class.它反映在 class 中的其他方法。

in HTML,在 HTML,

<mat-autocomplete #auto="matAutocomplete" autoActiveFirstOption [displayWith]="itemDisplayFn" (optionSelected)="selected($event)"> <mat-autocomplete #auto="matAutocomplete" autoActiveFirstOption [displayWith]="itemDisplayFn" (optionSelected)="selected($event)">

then for method itemDisplayFn(item: ..) in the ts file, the modeCity is undefined.然后对于 ts 文件中的方法 itemDisplayFn(item: ..),modeCity 未定义。

I found that somehow the method itemDisplayFn() has static context.我发现 itemDisplayFn() 方法以某种方式具有 static 上下文。 Therefore I created the property,因此我创建了属性,

static staticModeCity = false; static staticModeCity = false;

staticModeCity can be set in the ngOnInit() like so, staticModeCity 可以像这样在 ngOnInit() 中设置,

MyClass.staticModeCity = true

and used in the Method itemDisplayFn() like so,并像这样在方法 itemDisplayFn() 中使用,

if(MyClass.staticModeCity)....

I do not know why this is.我不知道这是为什么。 Of course static can be conflicting, if the same component is used multiple times in the same parent component.当然 static 可能会发生冲突,如果在同一个父组件中多次使用同一个组件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Angular 5 在 Angular Material Autocomplete 显示中绑定“this” - Binding 'this' in Angular Material Autocomplete displayWith using Angular 5 垫子自动完成中的无限滚动 angular 11 - Infinite scroll in mat-autocomplete angular 11 无法从 angular 材料自动完成中的 [displayWith] 调用另一个方法 - unable to call another method from [displayWith] in angular material autocomplete 如何显示名称但从 mat-autocomplete select 的对象中获取某些其他属性的值? 角-角材料 - How to display a name but take the values of some other property from the object from mat-autocomplete select? Angular- Angular Material Angular 6:如何访问 mat-autocomplete 下拉列表中的所有选项值? - Angular 6: how to access ALL option values in mat-autocomplete dropdown? 如何检测在 Angular 中选择了 mat-autocomplete 选项? - How to detect mat-autocomplete option is selected in Angular? 如何从另一个组件更新 mat-autocomplete 选项? - How to update mat-autocomplete options from another component? Angular5 - 如何检测垫子自动完成中的空字段或已删除字段? - Angular5 - How to detect empty or deleted field in mat-autocomplete? Angular 9 mat-autocomplete 不能与 switchmap 运算符一起正常工作 - Angular 9 mat-autocomplete not working properly with switchmap operator git与npm有什么关系? | npm ERR! 使用git失败。 | material.angular.io - How does git relate with npm? | npm ERR! Failed using git. | material.angular.io
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM