无法绑定到 ‘input’ 的 ‘form’,因为它不是 ‘input’ 的已知属性。ngtsc(-998002)

huangapple go评论115阅读模式
英文:

Using a native HTML element attribute dynamically in Angular - Can't bind to 'form' since it isn't a known property of 'input'.ngtsc(-998002)

问题

我正在尝试在Angular模板中设置一个表单,就像在示例中所示"HTML Input form* Attributes" - w3schools

<form action="/action_page.php" id="form1">
...
</form>

...
<input type="text" id="lname" name="lname" form="form1">

重要的部分是最后一行的 form="form1" 和第一行的 id="form1",它们将 <input> 元素与 <form> 元素链接起来,而不必将 <input> 嵌套在 <form> 中。

当我尝试在我的Angular HTML模板中使用硬编码值进行更新时,一切正常:

<input form="my-form-id">

但我需要动态更新它:

<input [form]="dynamicVariable">

不幸的是,使用方括号的 [form] 属性会导致以下错误:

Can't bind to 'form' since it isn't a known property of 'input'.ngtsc(-998002)

是否有一种方法可以在Angular模板中动态使用 form 属性?

英文:

I'm trying to set up a form in an Angular template, as per the example given in
"HTML Input form* Attributes" - w3schools:

<form action="/action_page.php" id="form1">
...
</form>

...
<input type="text" id="lname" name="lname" form="form1">

The important bits are the form="form1" on the last line and the id="form1" on the first line, which link the <input> element to the <form> element, without having to have the <input> nested within the <form>.

When I try to update my Angular HTML template with a hardcoded value, everything works fine:

<input form="my-form-id">

But I need to update it dynamically:

<input [form]="dynamicVariable">

Unfortunately, using the [form] attribute with square brackets causes the following error:

Can't bind to 'form' since it isn't a known property of 'input'.ngtsc(-998002)

Is there a way to use the form attribute dynamically in an Angular template?

答案1

得分: 1

使用以下代码替代:

<input [attr.form]="dynamicVariable">

尽管我认为这在实际的Angular用法中可能不起作用。例如,如果我尝试以下代码:

<td>
  <form [id]="tagMappingForm.value.tagName" [formGroup]="tagMappingForm">      
    <input formControlName="childrenTags">
  </form>
</td>
<td>
  <input [attr.form]="tagMappingForm.value.tagName" formControlName="label">
</td>

第一个输入框运行正常,但第二个输入框会导致错误:

NG01050: formControlName必须与父级formGroup指令一起使用。您需要添加一个formGroup。
英文:

Instead of

<input [form]="dynamicVariable">

Use

<input [attr.form]="dynamicVariable">

Although I don't think this works for practical Angular usage. For example, if I try:

  <td>
    <form [id]="tagMappingForm.value.tagName" [formGroup]="tagMappingForm">      
      <input formControlName="childrenTags">
    </form>
  </td>
  <td>
    <input [attr.form]="tagMappingForm.value.tagName" formControlName="label">
  </td>

The first input works fine, but the second input causes an error:

NG01050: formControlName must be used with a parent formGroup directive.  You'll want to add a formGroup

答案2

得分: -1

在这里,使用AfterViewInit生命周期钩子来使用纯JavaScript将输入元素的form属性设置为表单元素。像往常一样,在组件类中创建您的表单组:

import { Component, AfterViewInit, ElementRef } from '@angular/core';

@Component({
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
})
export class MyComponent implements AfterViewInit {
  constructor(private el: ElementRef) {}

  ngAfterViewInit() {
    const formElement = this.el.nativeElement.querySelector('#my-form');
    const inputElement = this.el.nativeElement.querySelector('#lname');

    inputElement.form = formElement;
  }
}

您的模板将分别包含表单和输入元素:

<form id="my-form">
  <!-- ... 其他表单控件 ... -->
</form>

<input type="text" id="lname">
英文:

Here , AfterViewInit lifecycle hook is used to set the form property of the input element to the form element using plain JavaScript.
Create your form group as usual in your component class:

import { Component, AfterViewInit, ElementRef } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-my-component&#39;,
  templateUrl: &#39;./my-component.component.html&#39;,
})
export class MyComponent implements AfterViewInit {
  constructor(private el: ElementRef) {}

  ngAfterViewInit() {
    const formElement = this.el.nativeElement.querySelector(&#39;#my-form&#39;);
    const inputElement = this.el.nativeElement.querySelector(&#39;#lname&#39;);

    inputElement.form = formElement;
  }
}

Your template will have the form and input element separately:

&lt;form id=&quot;my-form&quot;&gt;
  &lt;!-- ... other form controls ... --&gt;
&lt;/form&gt;

&lt;input type=&quot;text&quot; id=&quot;lname&quot;&gt;

huangapple
  • 本文由 发表于 2023年8月10日 17:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定