<input {{field.validatorDirective}}
class="form-control"
[ngClass]="{ 'border-danger': hasErrors }"
(keyup)="callback()"
[formControlName]="field.key"
[id]="field.key"
[type]="field.type"
[placeholder]="field.placeholder"
[value]="field.value">
field
拥有所需的所有内容,但我想动态添加要使用的指令名。
它目前作为string
添加为field.validatorDirective
。
这有错误
TextComponent.html:2错误DOM异常:未能对“元素”执行“setAttribute”:“{field.validatorDirective}”不是有效的属性名。
我的指令有选择器usernameValidator
发布于 2018-11-23 12:56:13
看来你不能这么做。我确实尝试过使用[attr.directiveSelector]="condition"
,但也不适用于此。
解决方案(如果您对其中一个感兴趣的话)是在两个输入上都有一个*ngIf
。就像这样:
<input directive1
*ngIf="condition1"
class="form-control"
[ngClass]="{ 'border-danger': hasErrors }"
(keyup)="callback()"
[formControlName]="field.key"
[id]="field.key"
[type]="field.type"
[placeholder]="field.placeholder"
[value]="field.value">
<input directive2
*ngIf="condition2"
class="form-control"
[ngClass]="{ 'border-danger': hasErrors }"
(keyup)="callback()"
[formControlName]="field.key"
[id]="field.key"
[type]="field.type"
[placeholder]="field.placeholder"
[value]="field.value">
https://stackoverflow.com/questions/53447021
复制相似问题