javascript - 推入 Angular 无法产生预期的结果
问题描述
我是角度学习推动力的新手。
我正在使用文本框将数据添加到表中。并通过单击按钮将模板引用传递给组件。
在组件中,我正在使用文本框的值和推送功能,但我无法检索数据
HTML 代码
<input type="text" #userName >
<button (click)="addToTable(userName)">Add To Table</button>
<table>
<tbody>
<tr *ngFor="let u of users">
<td>{{u.name}}</td>
</tr>
</tbody>
</table>
零件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-ng-if',
templateUrl: './ng-if.component.html',
styleUrls: ['./ng-if.component.css']
})
export class NgIfComponent implements OnInit {
selectedProduct=[];
constructor() {
this.selectedProduct;
}
addToTable(userName:any){
this.selectedProduct.push(
{name:userName.value}
);
}
ngOnInit() {}
}
解决方案
export class AppComponent {
users = [
{name: 'shan'}
]
addToTable(data){
this.users.push(
{name: data.value}
)
data.value = ''
}
}
您需要在users
数组中推送值而不是selectedProduct
推荐阅读
- performance - 通过别名在 Elasticsearch 中更新单个文档的最高效方式
- firebase - 在 firebase 和 vue 中处理异步数据加载的最佳方法
- google-chrome-devtools - 控制台替代品
- sql - 显示两个表之间不同的行 - MS Access
- node.js - 如何将 ansible 配置为在流式 api 上不超时
- python - 将灰度 2D numpy 图像投影到 RGB 中?
- java - Java 代码没有在 Android Studio 中启动新活动?和上下文有关吗?
- reactjs - 如何在 Framer Motion ReactJS 的 onClick 中使用两个函数
- flutter - Flutter BLoC:Cubits 比 BLoC 好吗?
- angular - 无法使用 Visual Studio 2019 安装 node-sass