[英]Trying to use ngModel inside of ng-template
I am trying to use ng-template
and ng-container
, alongside primeng
to have dynamic cells in my table:我正在尝试使用
ng-template
和ng-container
以及primeng
在我的表中包含动态单元格:
<tr *ngFor="let data of tableData">
<ng-container
[ngTemplateOutletContext]="{ $implicit: data.n0 }"
[ngTemplateOutlet]="isEditable ? editableCell : readOnlyCell"
></ng-container>
</tr>
<ng-template let-data #editableCell>
<td pEditableColumn>
<p-cellEditor>
<ng-template pTemplate="input">
<input
class="edit-field-input"
type="number"
[(ngModel)]="data"
/>
</ng-template>
</p-cellEditor>
</td>
</ng-template>
<ng-template let-data #readOnlyCell>
<td>
<p-cellEditor>
<ng-template pTemplate="output">
{{ data }}
</ng-template>
</p-cellEditor>
</td>
</ng-template>
But I get the error: Error Cannot assign value $event to template variable data. Template variables are read-only.
但我收到错误:
Error Cannot assign value $event to template variable data. Template variables are read-only.
Error Cannot assign value $event to template variable data. Template variables are read-only.
I believe this is related to my ngModel but I don't really know how to fix this.我相信这与我的 ngModel 有关,但我真的不知道如何解决这个问题。
The problem was on the way I was passing the data to the templates.问题出在我将数据传递给模板的过程中。
<tr *ngFor="let data of tableData">
<ng-container
[ngTemplateOutletContext]="{ row: data, name: 'n0' }"
[ngTemplateOutlet]="isEditable ? editableCell : readOnlyCell"
></ng-container>
</tr>
<ng-template let-row="row" let-name="name" #readOnlyCell>
<td class="degrees-cell">
{{ row[name] }}
</td>
</ng-template>
<ng-template let-row="row" let-name="name" #editableCell>
<td pEditableColumn class="degrees-cell">
<p-cellEditor>
<ng-template pTemplate="input">
<input
class="edit-field-input"
type="number"
[(ngModel)]="row[name]"
/>
</ng-template>
<ng-template pTemplate="output">
{{ row[name] }}
</ng-template>
</p-cellEditor>
</ng-template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.