【问题标题】:passing ngmodel throug a div element通过 div 元素传递 ngmodel
【发布时间】:2021-03-13 02:28:23
【问题描述】:

我的表中有一个值。我正在尝试将特定的单元格值传递给打字稿。但是当我使用 ngmodel 尝试它并且当我调试它时它变得未定义。 ngmodel 值 qty 在 typescript 中被指定为数字,它是一个数字。 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>

【问题讨论】:

标签: javascript angular typescript angular5 angular8


【解决方案1】:

你可以在html模板中使用(click)

<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

【讨论】:

  • 没有。将其作为 html 中的数字传递。将数字分配给 ts 中的 qty,然后在 ts 中的 qty 中获取该值 @sevic
  • 想象一下我从表格单元格中获取这些值
  • 您想在什么时候将表的值分配给qty?单击元素时?
  • 是的,当单击元素时,该 td 标记中的值应传递给打字稿 @sevic 中的 qty 变量
【解决方案2】:

尝试 [innerHTML] 而不是 [(ngModel)] :

<td>
  <div *ngIf="data === 'RARE' ; else notype" style="cursor: pointer;"
   (click)="View()" [innerHTML]="qty" name="fieldName" ngDefaultControl></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2020-02-18
    • 2016-04-09
    • 2011-07-20
    相关资源
    最近更新 更多