我有以下HTML但我得到了例外。如何解决?
分析器错误:得到插值({{}}),其中表达式是预期的 在第48栏 [!(editForm.controls.field_item_exportExpression _ {{i}} ?. dirty&& editForm.controls.field_item_exportExpression _ {{I}}?无效)]
<div class="form-group">
<label class="form-control-label" for="field_exportExpression">exportExpression</label>
<input class="form-control" type="text" id="field_item_exportExpression_{{i}}" name="item_exportExpression_{{i}}" [(ngModel)]="datatype.items[i].exportExpression" required>
<div [hidden]="!(editForm.controls.field_item_exportExpression_{{i}}?.dirty && editForm.controls.field_item_exportExpression_{{i}}?.invalid)">
<small class="form-text text-danger" [hidden]="!editForm.controls.field_item_exportExpression_{{i}}?.errors?.required" dpTranslate="dataconfiguration.validation.required"> This field is
required. </small>
</div>
</div>
以下不起作用。说找到不需要的令牌[
。
[hidden]="!editForm.controls.['item_exportExpression_' + i]?.errors?.required
以下内容并非抱怨[
,而是抱怨Cannot read property '0' of undefined
[hidden]="!editForm.controls.item_exportExpression_[ i]?.errors?.required
答案 0 :(得分:51)
{{}}
从不与[prop]="..."
或(event)="..."
<small class="form-text text-danger"
[hidden]="!editForm.controls.['field_item_exportExpression_' + i]?.errors?.required" dpTranslate="dataconfiguration.validation.required"> This field is
required.
</small>
答案 1 :(得分:24)
有4种类型的绑定:
[]
[()]
无需任何特殊内容{{}}
可与常规属性一起使用()
与函数要回答你的问题,这样的事情对我们有用:
<input type="checkbox" [id]="['model-'+i]" [(ngModel)]="model" name="model-{{i}}" (change)="changeHandler($event)" />
答案 2 :(得分:9)
模板
<div [hidden]="!checkIfInvalid(i, 'item_exportExpression_')">
<small class="form-text text-danger" [hidden]="isRequiredError(i, 'item_exportExpression_')" dpTranslate="dataconfiguration.validation.required"> This field is
required. </small>
</div>
成分</ P>
checkIfInvalid( index: number, field: string ): boolean {
const control = this.getControl( index, field );
if ( control && control.dirty && !control.valid ) {
return true;
}
return false;
}
isRequiredError( index: number, field?: string ): boolean {
const control = this.getControl( index, field );
if ( control && control.getError( "required" ) ) {
return true;
}
return false;
}
答案 3 :(得分:0)
像这样使用而不是插值
<button class="btn btn-primary" title="Edit" (click)="showEditModal(record.id)"><i class="fa fa-edit"></i></button>