javascript - 通过 div 元素传递 ngmodel
问题描述
我的表中有一个值。我正在尝试将特定的单元格值传递给打字稿。但是当我使用ngmodel尝试它并且当我调试它时它变得未定义。ngmodel 值qty在打字稿中被分配为数字,它是一个数字。stackblitz 仅供参考,实际上我从列表中获得的 td 标签中获取值
Stackblitz 示例一: https ://stackblitz.com/edit/angular-ivy-sundu2?file=src%2Fapp%2Fapp.component.html
我的代码:
<td>
<div *ngIf="data === 'RARE' ; else notype" style="cursor: pointer;"
(click)="View()" [(ngModel)]="qty" name="fieldName" ngDefaultControl>{{ list.value}}</div>
<ng-template #notype (click)="View()"> {{currentvalue}}</ng-template>
</td>
解决方案
您可以(click)
在 html 模板中使用
<td>
<div *ngIf="data === 'RARE' ; else notype" style="cursor: pointer;"
(click)="setQty(list.value)" name="fieldName" ngDefaultControl>{{ list.value}}</div>
<ng-template #notype (click)="View()"> {{currentvalue}}</ng-template>
</td>
并在组件中
setQty(val: number) {
this.qty = val;
}
Stackblitz:https ://stackblitz.com/edit/angular-ivy-gavpqc?file=src/app/app.component.ts
有关更多详细信息,请参阅:https ://angular.io/guide/event-binding
推荐阅读
- snmp - 网络 SNMP 主机 snmpwalk
- python - 向现有 conda 环境添加新库
- python - 简单的python tensorflow InternalError:无法将元素作为字节获取
- spring - @Primary 即时
- vb.net - 不确定如何解释 datagridview 中的项目组和计数列表
- javascript - 使用 Vuex 和 Firebase 编辑项目
- sql - Case 和 Where 逻辑看起来相似但产生不同的结果
- java - Netbeans 项目无法运行
- c# - C# Web 应用程序“当前上下文中不存在名称‘CheckBox2’”
- java - Java 将比较器与自定义比较器结合使用