Angular表单包含应该创建一个具有其他JSON对象作为属性的JSON对象的输入值。

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

Angular Form Containing input values that should create a JSON object with other JSON objects as atributes

问题

我尝试创建一个表单,该表单将作为其值返回一个包含对象列表的JSON对象作为属性。表单值的JSON示例:

{
    "name": "contestName",
    "teams": [
        {
            "name": "team1",
            "wins": 1,
            "loses": 1
        },
        {
            "name": "team2",
            "wins": 2,
            "loses": 2
        }
    ]
}

我的表单如下:

<form id="form" #ContestForm="ngForm" (ngSubmit)="addNewContest(ContestForm.value)">
    <div class="form-group" [(ngModel)]="contest">
        <input type="text" [(ngModel)]="name" class="form-control" placeholder="Contest Name">
    </div>

    <div class="form-group" [(ngModel)]="teams" *ngFor="let i of [].constructor(2)">
        <label>Team {{i}}:</label>
        <input type="text" [(ngModel)]="name" class="form-control" placeholder="Name">
        <div class="form-group">
            <input type="number" [(ngModel)]="wins" class="form-control" placeholder="Wins">
        </div>
        <div class="form-group">
            <input type="number" [(ngModel)]="loses" class="form-control" placeholder="Loses">
        </div>
    </div>
</form>

我尝试过使用typescript中的createObjectFromForm函数设置值,该函数将单独分配值并返回对象,但没有结果。我希望有人面对过这个问题,因为我认为这是一个相当常见的问题。

英文:

Im trying to create a form that will return as its value an JSON object that contains as an attribute a list of objects. Form value JSON example :

{
    &quot;name&quot; : &quot;contestName&quot;,
    &quot;teams&quot; :[
        {
          &quot;name&quot;:&quot;team1&quot;,
          &quot;wins&quot;:1,
          &quot;loses&quot;:1},
        {
          &quot;name&quot;:&quot;team2&quot;,
          &quot;wins&quot;:2,
          &quot;loses&quot;:2}
             ]

}

My Form Looks like :

  &lt;form id=&quot;form&quot; #ContestForm=&quot;ngForm&quot;  (ngSubmit)=&quot;addNewContest(ContestForm.value)&quot;&gt;
          &lt;div class=&quot;form-group&quot; [(ngModel)]=&quot;contest&quot; &gt;
            &lt;input type=&quot;Contest Name&quot; [(ngModel)]=&quot;name&quot;  class=&quot;form-control&quot; placeholder=&quot;Contest Name&quot;&gt;

          &lt;br&gt;

          &lt;div class=&quot;form-group&quot; [(ngModel)]=&quot;teams&quot;  *ngFor=&quot;let i of [].constructor(2)&quot;&gt;
            &lt;br&gt;
            &lt;label&gt;Team {{i}} :&lt;/label&gt;
            &lt;br&gt;&lt;br&gt;
            &lt;input type=&quot;Name&quot; [(ngModel)]=&quot;name&quot;  class=&quot;form-control&quot; placeholder=&quot;Name&quot;&gt;

          &lt;br&gt;
          &lt;div class=&quot;form-group&quot;&gt;
            &lt;input type=&quot;Wins&quot; [(ngModel)]=&quot;wins&quot;  class=&quot;form-control&quot; placeholder=&quot;Wins&quot;&gt;
          &lt;/div&gt;
          &lt;br&gt;
          &lt;div class=&quot;form-group&quot;&gt;
            &lt;input type=&quot;Loses&quot; [(ngModel)]=&quot;loses&quot;  class=&quot;form-control&quot; placeholder=&quot;Loses&quot;&gt;
          &lt;/div&gt;
        &lt;/div&gt;

        &lt;br&gt;

        &lt;/div&gt;


        &lt;/form&gt;

I tried this and setting the values through typescript in a createObjectFromForm function that would individually assign the values and return the object but got no results, I am hoping someone faced this issue as I think it`s a pretty common one.

答案1

得分: 0

感谢Brandon Taylor,我使用了响应式表单和表单组,达到了期望的结果!示例:

表单(在HTML文件中):

<form [formGroup]="contestForm" (ngSubmit)="alert()">
    <input formControlName="name"/>
    <div formArrayName="teams" *ngFor="let team of getTeamsControls(); index as i">
        <div formGroupName="{{i}}">
            <label>姓名</label>
            <input type="text" id="name" name="name" formControlName="name">
            <label>胜利</label>
            <input type="text" id="wins" name="wins" formControlName="wins">
            <label>失败</label>
            <input type="text" id="loses" name="loses" formControlName="loses">
        </div>
    </div>
    <p>
        <button type="submit">提交</button>
    </p>
</form>

表单声明和getTeamsControls()函数(在TypeScript文件中):

contestForm = new FormGroup({
    name: new FormControl(''),
    teams: new FormArray([
        new FormGroup({
            name: new FormControl(''),
            wins: new FormControl(''),
            loses: new FormControl('')
        }),
        new FormGroup({
            name: new FormControl(''),
            wins: new FormControl(''),
            loses: new FormControl('')
        })
    ])
})

getTeamsControls() {
    return (<FormArray>this.contestForm.get('teams')).controls;
}

请注意,以上是您提供的代码的翻译部分。

英文:

Thanks Brandon Taylor, I used a reactive form and form groups and reached the desired result! example :

form(in html file) :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

 &lt;form [formGroup]=&quot;contestForm&quot; (ngSubmit)=&quot;alert()&quot;&gt;

          &lt;input formControlName=&quot;name&quot;/&gt;

          &lt;div formArrayName=&quot;teams&quot; *ngFor=&quot;let team of getTeamsControls(); index as i&quot;&gt;

          &lt;div formGroupName=&quot;{{i}}&quot;&gt;

            &lt;label&gt; name &lt;/label&gt;
            &lt;input type=&quot;text&quot; id=&quot;name&quot; name=&quot;name&quot; formControlName=&quot;name&quot;&gt;

            &lt;label&gt; wins &lt;/label&gt;
            &lt;input type=&quot;text&quot; id=&quot;wins&quot; name=&quot;wins&quot; formControlName=&quot;wins&quot;&gt;

            &lt;label&gt; loses &lt;/label&gt;
            &lt;input type=&quot;text&quot; id=&quot;loses&quot; name=&quot;loses&quot; formControlName=&quot;loses&quot;&gt;

            &lt;/div&gt;

          &lt;/div&gt;

          &lt;p&gt;
            &lt;button type=&quot;submit&quot;&gt;Submit&lt;/button&gt;
          &lt;/p&gt;

        &lt;/form&gt;

<!-- end snippet -->

form declaration and getTeamsControls(); function (in typescript file):

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-typescript -->

  contestForm = new FormGroup({

    name: new FormControl(&#39;&#39;),

    teams: new FormArray([

      new FormGroup({

          name : new FormControl(&#39;&#39;),
          wins : new FormControl(&#39;&#39;),
          loses : new FormControl(&#39;&#39;)


      })
      ,
      new FormGroup({

          name : new FormControl(&#39;&#39;),
          wins : new FormControl(&#39;&#39;),
          loses : new FormControl(&#39;&#39;)
      })
   ])
  })

  getTeamsControls() { return (&lt;FormArray&gt;this.contestForm.get(&#39;teams&#39;)).controls;}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 02:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354482.html
匿名

发表评论

匿名网友

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

确定