如何在模板中以角色形式访问动态的输入名称属性
我将 ngx-datatable包装在form> form
标签中,以便在表单元格中验证inputs
。由于如何填充表的性质,我设置了输入name
属性,属性属性正常
<form #tableForm="ngForm">
<ngx-datatable
[rows]="_rows">
<ng-container *ngFor="let column of rowDeffinition; let columnIndex=index">
<ngx-datatable-column [prop]="column.key" [name]="column.label">
<ng-template ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value" let-row="row">
<input
class="cell-input"
(blur)="updateCellValue($event, column.key, rowIndex)"
type="text"
[ngModel]="value"
[name]="rowIndex + '-' + column.key"
/>
...
</ng-template>
</ngx-datatable-column>
</ng-container>
</ngx-datatable>
</form>
,name
属性将在模板中创建局部变量,您可以访问输入通过变量名称控制属性。
<input type="text" name="name" [(ngModel)]="name" required minlength="4" />
<div *ngIf="name.invalid && name.touched">
我想知道如何以相同的方式动态执行此操作。到目前为止,我能够通过表格参考访问输入控件
<span *ngIf="!tableForm.controls[rowIndex + '-' + column.key]?.valid &&
!tableForm.controls[rowIndex + '-' + column.key]?.pristine"
class="[ c-validation-message ]">
required
</span>
I wrapped an ngx-datatable component in a form
tag so I can validate inputs
in the table cells. Due to the nature of how the table is populated I set the inputs name
properties dynamically
<form #tableForm="ngForm">
<ngx-datatable
[rows]="_rows">
<ng-container *ngFor="let column of rowDeffinition; let columnIndex=index">
<ngx-datatable-column [prop]="column.key" [name]="column.label">
<ng-template ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value" let-row="row">
<input
class="cell-input"
(blur)="updateCellValue($event, column.key, rowIndex)"
type="text"
[ngModel]="value"
[name]="rowIndex + '-' + column.key"
/>
...
</ng-template>
</ngx-datatable-column>
</ng-container>
</ngx-datatable>
</form>
Normally, the name
property would creates a local variable in the template and you can access the inputs control properties via the variable name.
<input type="text" name="name" [(ngModel)]="name" required minlength="4" />
<div *ngIf="name.invalid && name.touched">
I wonder how can I do this dynamically in the same manner I set the inputs name. So far I was able to access the input controls via the form reference but this becomes quite wordy
<span *ngIf="!tableForm.controls[rowIndex + '-' + column.key]?.valid &&
!tableForm.controls[rowIndex + '-' + column.key]?.pristine"
class="[ c-validation-message ]">
required
</span>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将输入包装到新组件中,并且可以通过
@viewchildren(...)
访问这些生成的组件 in Parent Components.ts
file> File:我建议在中创建方法父组件,从
datatableInputs
byname
作为参数中检索混凝土datatableInput。之后,您可以在生成的,新的验证pancomponent
中使用此方法:verialationspancomponent的模板
:You can wrap your input in new component and you can access these generated components via
@ViewChildren(...)
in parent components.ts
file:I recommend to create method in parent component, which retrieves concrete datatableInput from
datatableInputs
byname
as parameter. After that you can use this method in generated, newValidationSpanComponent
:Template of
ValidationSpanComponent
:灵感来自这个答案我想出了以下解决方案
,这可以极大地提高一般的可读性,因为我的表具有非常复杂的逻辑,很复杂,我在类似结构中跟踪单元格的状态:
这导致模板中的超级复杂属性绑定
到现在变得更加可读性
Inspired by this answer I came up with following solution
This improves the general readability vastly, since my table has a very complex logic, and i track the state of the cell in a structure like:
which led to super complex property binding in the template
now becoming much more readable