仅使用一个按钮获取倍数的值

发布于 2025-02-11 21:20:48 字数 2570 浏览 0 评论 0原文

我正在尝试同时获得倍数的结果... 目前,我有一个结构可以一个接一个地获得结果,但是我需要发送所有结果...

我的HTML代码:

<form (ngSubmit)="onSubmit(f)" #f="ngForm" *ngFor="let x of selectedValue; index as i">
                    <mat-card > 
                        <mat-card-title class="title-card">
                            <span>{{x.name}}</span>
                        </mat-card-title>
                        <mat-card-content *ngFor="let param of x.modelParameter; index as i">
                            <div class="param-title">
                                <span><b>{{param.name}}</b></span>
                            </div>
                            <div class="set-slider" *ngIf="param.type === 'continuous'"> 
                                <label class="gridSizeValue"><b>{{param.values_arr[0]}}</b></label>
                                <mat-slider #gridsize (change)="updateSetting($event)" class="slider" thumbLabel
                                    tickInterval="1" [displayWith]="formatLabel" min="{{param.values_arr[0]}}"
                                    max="{{param.values_arr[1]}}" aria-label="units" class="form-control" ngModel name="{{param.name}}">
                                </mat-slider>
                                <label class="gridSizeValue"><b>{{param.values_arr[1]}}</b></label>
                            </div>
                            <div class="set-categorical" *ngIf="param.type === 'categorical'">
                                <mat-form-field appearance="outline">
                                    <mat-select 
                                    class="form-control"
                                    ngModel name="option">
                                        <mat-option *ngFor="let op of param.values_arr" [value]="op">
                                            {{op}}
                                        </mat-option>
                                    </mat-select>
                                </mat-form-field>
                            </div> 
                        </mat-card-content>
                    </mat-card>
                    <button mat-raised-button type="submit" class="save-button">Send Models</button>
                </form>

我试图将按钮放在外部方法中,但它不起作用,而且我也很试图通过ngmodel获得值,但它也不起作用。

你有建议吗?

I'm trying to get the result for multiples forms at same time...
Currently, I have a structure to get the result one by one but I need to send the result of all of them...

My html code:

<form (ngSubmit)="onSubmit(f)" #f="ngForm" *ngFor="let x of selectedValue; index as i">
                    <mat-card > 
                        <mat-card-title class="title-card">
                            <span>{{x.name}}</span>
                        </mat-card-title>
                        <mat-card-content *ngFor="let param of x.modelParameter; index as i">
                            <div class="param-title">
                                <span><b>{{param.name}}</b></span>
                            </div>
                            <div class="set-slider" *ngIf="param.type === 'continuous'"> 
                                <label class="gridSizeValue"><b>{{param.values_arr[0]}}</b></label>
                                <mat-slider #gridsize (change)="updateSetting($event)" class="slider" thumbLabel
                                    tickInterval="1" [displayWith]="formatLabel" min="{{param.values_arr[0]}}"
                                    max="{{param.values_arr[1]}}" aria-label="units" class="form-control" ngModel name="{{param.name}}">
                                </mat-slider>
                                <label class="gridSizeValue"><b>{{param.values_arr[1]}}</b></label>
                            </div>
                            <div class="set-categorical" *ngIf="param.type === 'categorical'">
                                <mat-form-field appearance="outline">
                                    <mat-select 
                                    class="form-control"
                                    ngModel name="option">
                                        <mat-option *ngFor="let op of param.values_arr" [value]="op">
                                            {{op}}
                                        </mat-option>
                                    </mat-select>
                                </mat-form-field>
                            </div> 
                        </mat-card-content>
                    </mat-card>
                    <button mat-raised-button type="submit" class="save-button">Send Models</button>
                </form>

I was trying to put the button outside method but it does not work, and also I was trying to get the value by ngModel but it does not works neither.

Do you have any suggest?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2025-02-18 21:20:48

当您在循环中具有模板参考变量时,当您使用此变量时,将“范围”对每个迭代进行“范围”。

您可以使用ViewChildren来获取所有元素具有相同的“模板参考变量”

  @ViewChildren('f') forms:QueryList<NgForm>

一个按钮类型在循环外提交可以调用函数的元素(您可以拨打提交或以其他方式调用)

   <button (click)="submit()">

您的功能提交,

submit()
{
   const isInvalid=this.forms.map(x=>x.valid).find(x=>!x)
   //isInvalid is "undefined" if all are valid
   //          is true if one of the forms is invalid

   if (!isInvalid)
   {
       this.forms.forEach(x=>{
         console.log(x.value)
     })
   }
}

现在我们得到表单。您如何创建数据?

//in array
submit()
{
   ...
   const data=this.forms.map(x=>x.value)    

}
//in an object
submit()
{
   ...
   const obj:any={}
   this.forms.forEach((x,i)=>{
      obj['form'+i]=x.value
   })
}

//if the "name" of the controls are all differents
//using spread operator
submit()
{
   ...
   let obj:any={}
   this.forms.forEach((x,i)=>{
      obj={...obj,...x.value}
   })
}

顺便说一句,您想看一下使用 [(ngmodel)] 使用数组或< a href =“ https://angular.io/guide/reeactive-forms” rel =“ nofollow noreferrer”> reactiveforms

When you has a template reference variable in a loop, when you use this variable is "scoped" to each iteration.

You can use ViewChildren to get all elements with the same "template reference Variable"

  @ViewChildren('f') forms:QueryList<NgForm>

A button type submit outside the loop can call to a function (you can call submit or in another way)

   <button (click)="submit()">

You function submit

submit()
{
   const isInvalid=this.forms.map(x=>x.valid).find(x=>!x)
   //isInvalid is "undefined" if all are valid
   //          is true if one of the forms is invalid

   if (!isInvalid)
   {
       this.forms.forEach(x=>{
         console.log(x.value)
     })
   }
}

Now we get the forms the question is: how you can create your data?

//in array
submit()
{
   ...
   const data=this.forms.map(x=>x.value)    

}
//in an object
submit()
{
   ...
   const obj:any={}
   this.forms.forEach((x,i)=>{
      obj['form'+i]=x.value
   })
}

//if the "name" of the controls are all differents
//using spread operator
submit()
{
   ...
   let obj:any={}
   this.forms.forEach((x,i)=>{
      obj={...obj,...x.value}
   })
}

BTW perhafs you want to take a look to use [(ngModel)] using an array or ReactiveForms

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文