【问题标题】:Angular2: use [(ngModel)] with [ngModelOptions]="{standalone: true}" to link to a reference to model's propertyAngular2:使用 [(ngModel)] 和 [ngModelOptions]="{standalone: true}" 链接到对模型属性的引用
【发布时间】:2016-11-16 20:56:29
【问题描述】:

假设我有一个 Mailtype 类型的打字稿对象,如下所示:

export class Mailtype {
  constructor(
    public name?: string,
    public locale?: string,
    public email?: string,
    public properties? : Property[]
  ) {  }
}

其中的“属性”字段是一个属性类型的数组:

export class Property {
  constructor(
    public name?: string,
    public type?: string,
    public example?: string,
    public required?: boolean,
    public masked?: boolean
  ) {  }
}

现在在我的组件中,我有一个 Mailtype 对象,并且 html 有一个表单元素,用于编辑和添加到 Mailtype 的属性数组中:

<form>
   <tr *ngFor="let property of model.properties; let i=index">
          <td>
            <input type="text" [(ngModel)]="property.name" required>
          </td>
  </tr>
  <button (click)="onAddProperty()">Add property</button>
</form>

组件:

export class MailtypeComponent {
  model : Mailtype;
  constructor() {
    this.model = new Mailtype('','','',[]);
    this.model.properties.push(new Property());
  }

  onAddProperty() {
    this.model.properties.push(new Property());
  }
}

我想知道是否不允许我使用 [(ngModel)] 链接到数组中数组元素的引用“属性”,尤其是在我迭代数组的同时?因为上面的代码会抛出如下错误:

ORIGINAL EXCEPTION: If ngModel is used within a form tag, either the name attribute must be set
                      or the form control must be defined as 'standalone' in ngModelOptions.

                      Example 1: <input [(ngModel)]="person.firstName" name="first">
                      Example 2: <input [(ngModel)]="person.firstName" [ngModelOptions]="{standalone: true}">

所以建议我使用 [ngModelOptions]="{standalone: true}" 或在 html 中添加名称字段。看起来[ngModelOptions]="{standalone: true}" 在这种情况下有效。 standalone: true 究竟是什么意思,因为我找不到任何关于它的文档?

【问题讨论】:

  • 请记住,表单中的所有组件或控件都应该是名称,也应该是外部控制器,如 Prime ng、ngBootstrap。 ExamplePrimeNg

标签: angular typescript angular-ngmodel


【解决方案1】:

当您使用&lt;form&gt; 标签时使用@angular/forms,它会自动创建一个FormGroup

对于每个包含的ngModel 标记&lt;input&gt;,它将创建一个FormControl 并将其添加到上面创建的FormGroup 中;此FormControl 将使用属性name 命名为FormGroup

例子:

<form #f="ngForm">
    <input type="text" [(ngModel)]="firstFieldVariable" name="firstField">
    <span>{{ f.controls['firstField']?.value }}</span>
</form>

说到这里,你的问题的答案如下。

当您将其标记为 standalone: true 时,这不会发生(它不会添加到 FormGroup)。

参考:https://github.com/angular/angular/issues/9230#issuecomment-228116474

【讨论】:

    【解决方案2】:

    对我来说代码:

    <form (submit)="addTodo()">
      <input type="text" [(ngModel)]="text">
    </form>

    抛出错误,但我在输入中添加了名称属性:

    <form (submit)="addTodo()">
      <input type="text" [(ngModel)]="text" name="text">
    </form>

    它开始起作用了。

    【讨论】:

      【解决方案3】:

      <form (submit)="addTodo()">
        <input type="text" [(ngModel)]="text">
      </form>

      【讨论】:

      • 欢迎来到 SO!请编辑您的答案,使其包含一些与问题相关的文字解释,...
      猜你喜欢
      • 2019-09-30
      • 2019-04-04
      • 2018-07-09
      • 1970-01-01
      • 2015-12-13
      • 2015-05-19
      • 1970-01-01
      • 2016-02-08
      • 2019-07-03
      相关资源
      最近更新 更多