首页 > 解决方案 > 通过 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>

标签: javascriptangulartypescriptangular5angular8

解决方案


您可以(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


推荐阅读