本文介绍了角度7-不会触发"valueChanges"从模板(使用mat-option)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单控件.在我的模板中,我使用mat-option对其进行了更改,但我希望此特定更改不会触发valueChanges(我还有其他更改,我希望将其称为"valueChanges").

I have a form control. In my template I am changing it using mat-option, but I want that this specific change will not trigger valueChanges (I have other changes that I want that "valueChanges" will be called).

我的组件-

inputControl = new FormControl('');


    ngOnInit() {
        this.inputControl.valueChanges
            .pipe(takeUntil(this.onDestroy))
            .subscribe(val => {
              //do something
            });
    }

html-

<ng-container *ngIf="inputControl.value as val">
        <mat-option *ngFor="let result of autocompleteResults; let i = index" [value]="result.displayName"
                    (onSelectionChange)="searchByResult($event, result)">
           {{result.displayName}}
        </mat-option>
    </ng-container>

你知道这是否可行吗?

推荐答案

您的mat-option必须包装在mat-select的内部,如下所示

Your mat-option must be wrapped inside of mat-select as below

  <mat-select>
    <mat-option *ngFor="let food of foods" [value]="food.value">
      {{food.viewValue}}
    </mat-option>
  </mat-select>

查看角材的文档.它有很好的示例代码片段

Checkout the documentation of angular material. It has pretty good examples with code snippets

在此链接: https://material.angular.io/components/select/overview

这篇关于角度7-不会触发"valueChanges"从模板(使用mat-option)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 05:15