gpt4 book ai didi

Angular 7 - 动态添加指令

转载 作者:太空狗 更新时间:2023-10-29 18:28:29 25 4
gpt4 key购买 nike

<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 ERROR DOM Exception: Failed to execute 'setAttribute' on 'Element': '{{field.validatorDirective}}' is not a valid attribute name.

我的指令有选择器 usernameValidator

最佳答案

看来你不能那样做。我确实尝试过使用 [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">

关于Angular 7 - 动态添加指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com