如何在 HTML 代码 Angular 中为 HTML 引用设置 类?

How to set classes for an HTML reference from within the HTML code Angular?

我有一个文本区域的引用

<*wrapper-component (submit)="tx1.value ? submit(tx1.value) : tx1.setClass('required')??">
<textarea #tx1> <textarea>
</*wrapper-component>

我将 提交函数的值传递给 ,是否可以为 [=24= 中的 tx1 元素设置 类 ] 本身而不是创建 ViewChild 属性 并使用 typeScript 访问 elRef.nativeElement.setClass?

我通常建议使用 [ngClass] 绑定来动态设置 CSS 选择器。但是,如果您坚持使用模板引用变量,则可以使用 setAttribute() 方法设置属性,在您的情况下为 'class'.

<*wrapper-component (submit)="tx1.value ? submit(tx1.value) : tx1.setAttribute('class', 'required')">
  <textarea #tx1> <textarea>
</*wrapper-component>