angular - 按钮的发送值不起作用 [Angular Material Bootstrap]
问题描述
嘿,我尝试将按钮的值发送到这样的函数
<button #mybtn value="somthing" (click)="filterTable(mybtn.value)" mat-stroked-button>mybtn</button>
在我的 ts 组件中:
filterTable(filterValue) {
console.log(filterValue);
}
它安慰 Undefiend
但是当我像这样在没有 Bootstrap 的情况下制作按钮时
<button #mybtn value="Somthing" (click)="filterTable(mybtn.value)">mytn</button>
这种方式是控制台“Somthing”并且可以工作。为什么???
解决方案
您需要访问本机元素以获取值。将此添加到您的 .ts 文件中
import { Component, ElementRef, ViewChild } from '@angular/core';
...
export class SomeComponent {
@ViewChild('mybtn', {static: false}) mybtn: ElementRef;
...
public filterTable() {
console.log(this.mybtn.nativeElement.value)
}
从您的方法中省略参数,logButton
使其变得只是
(click)="filterTable()"
推荐阅读
- java - java - 如何获取仅包含Java中特定字段的对象列表?
- java - 进度条未显示在 webview 的相对布局中
- mysql - MariaDB 多行插入顺序
- ios - Swift Firebase 如何在 TableView 中滑动删除消息
- c# - C# Win-Forms 用户控件后台任务执行延迟
- python - 在 Dataframe 中按列分组并为所有组创建单独的 csv
- wordpress - 从现有项目制作 Wordpress 子主题
- java - Eclipse 详细信息格式化程序字符串未显示所有 Unicode 字符
- python - 使用 python 烧瓶框架在 html 正文中设置背景图像
- regex - 使用正则表达式匹配使用 netstat -nb python 的进程名称