html - 选择mat-option(角度)时如何调用方法?
问题描述
我是 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';
}
}
解决方案
试试这个
<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>
在组件.ts
title = 'materialdesign';
selected = 'option33';
inputChange(){
if(this.selected == 'option3'){
this.greet();
}
}
greet() {
this.selected = 'it works';
}
推荐阅读
- javascript - 在 HTML5 Canvas 游戏中更改精灵的黑色像素颜色
- python - 如何在不手动设置 FLASK_APP 的情况下通过烧瓶运行或自定义命令运行 Flask 应用程序
- django - 从父级保存相关的子模型字段 - Django
- laravel - Laravel 与表的关系
- php - Timber Twig:按类别查询自定义帖子类型并输出帖子的简码
- airflow - 如何使用 xcom 变量获取任务输出?
- python - 卷积神经网络 (CNN) 可以用数学公式表示吗?
- javascript - 从数组上传文件
- javascript - Cloud Functions 实时数据库触发器中的未定义类型
- vue.js - 我可以在 vue.js 中将授权逻辑放在哪里?