【问题标题】:how to call a method when mat-option is selected (angular)?选择mat-option(角度)时如何调用方法?
【发布时间】:2020-04-25 22:27:18
【问题描述】:

我是 Angular 的新手,我想调用一个特定的方法,只有当 option3 被选中时。 我正在努力解决这个问题,在互联网上找不到太多关于此的信息。

当我调用option3时,输出为空。

到目前为止,这是我的代码:

app.component.html:

<mat-form-field>
  <mat-label>Select an option</mat-label>
  <mat-select [(value)]="selected" >
    <mat-option>None</mat-option>
    <mat-option value="option1">Option 1</mat-option>
    <mat-option value="option2">Option 2</mat-option>
    <mat-option selectionChange="greet($event)">Option 3</mat-option>
  </mat-select>
</mat-form-field>

<p>You selected: {{selected}}</p>

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'materialdesign';
  selected = 'option33';


  greet() {
    this.selected = 'it works';
  }
}

【问题讨论】:

    标签: html angular typescript binding angular-material


    【解决方案1】:

    试试这个

    <mat-form-field>
      <mat-label>Select an option</mat-label>
      <mat-select [(value)]="selected" (selectionChange)="inputChange()">
        <mat-option>None</mat-option>
        <mat-option value="option1">Option 1</mat-option>
        <mat-option value="option2">Option 2</mat-option>
        <mat-option value="option3">Option 3</mat-option>
      </mat-select>
    </mat-form-field>
    
    <p>You selected: {{selected}}</p>
    

    在component.ts中

    title = 'materialdesign';
      selected = 'option33';
    inputChange(){
      if(this.selected == 'option3'){
        this.greet();
      }
    }
    
      greet() {
        this.selected = 'it works';
      }
    

    stackblitz example

    【讨论】:

    • 谢谢,这也有效。只是有一个小错误:当我点击选项 3 时,输出会正确,但选择框不再显示选项 3。它只是空的。你知道怎么解决吗?它仍然存在,即使我为 mat-option 添加了一个值
    【解决方案2】:

    只需在特定的mat-option 元素上定义click 事件处理程序。

    <mat-option value="option3" (click)="greet()">Option 3</mat-option>
    

    【讨论】:

    • 谢谢,这也有效。只是有一个小错误:当我点击选项 3 时,输出是正确的,但选择框不再显示选项 3。它只是空的。你知道怎么解决吗?它仍然存在,即使我为 mat-option 添加一个值
    • 那是因为在 greet 方法中,你用一个不对应任何 mat-option 的值覆盖了 this.selected
    • 哦,是的,这是有道理的。知道如何解决这个问题吗?
    • greet 方法中删除this.selected = 'it works';。由于mat-select 上的双向绑定[(value)]="selected",属性this.selected 已经自动更新。
    【解决方案3】:

    如果你想监听来自 Angular 组件的事件,你需要在事件周围添加括号:

    <mat-option (selectionChange)="greet($event)">Option 3</mat-option>
    

    那么你的 should 方法应该开始执行了。

    【讨论】:

    • 谢谢,这也有效。只是有一个小错误:当我点击选项 3 时,输出是正确的,但选择框不再显示选项 3。它只是空的。你知道怎么解决吗?它仍然存在,即使我为 mat-option 添加一个值
    • 看起来其他人已经说过,当您将this.selected 的值更改为绑定到与mat-option 不匹配的值时,您将进入取消选择状态,因为selected 绑定到mat-selectvalue
    【解决方案4】:

    尝试如下,

    selectionChange 事件是 mat-select 的一部分。因此,当您从列表中选择一个选项时,只需调用该方法。并根据所选选项的值,执行您需要的代码。

    .html

    <mat-form-field>
      <mat-label>Select an option</mat-label>
      <mat-select [(value)]="selected" (selectionChange)="greet($event)">
        <mat-option>None</mat-option>
        <mat-option value="option1">Option 1</mat-option>
        <mat-option value="option2">Option 2</mat-option>
        <mat-option value="option3">Option 3</mat-option>
      </mat-select>
    </mat-form-field>
    

    .ts

    selected: string;
    
    greet(event) {
      if(event.value === 'option3') {
        // Execute the required code
      }
    }
    

    【讨论】:

    • 谢谢,它有效。只是有一个小错误:当我点击选项 3 时,输出会正确,但选择框不再显示选项 3。它只是空的。你知道为什么会这样吗?
    • 您忘记保留 option3 mat-option 的值属性。 选项3
    • 现在我很生气。我也应该更改最后一个垫子选项吗?你可以编辑你的答案吗?
    • 是的。将最后一个选项替换为 Option 3
    • 我做到了,但错误仍然存​​在。当我点击 option3 时,有正确的输出,但 Option3 不再显示。
    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2020-08-12
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多