简体   繁体   English

Angular:ngSubmit 未发送表单

[英]Angular: ngSubmit not sending form

Everything is working fine but when I click the submit button (ngSubmit) is not working.一切正常,但是当我单击提交按钮 (ngSubmit) 时,它不起作用。 FormsModule + ReactiveFormsModule are already imported and the submit button is inside the form. FormsModule + ReactiveFormsModule 已经导入,提交按钮在表单内。 Do you have any ideas?你有什么想法?

This is the html, ng-template #editMode will be called这是 html,ng-template #editMode 将被调用

<table>
    <tr class="tr_header">
        <th>Name</th>
        <th>Weight in [kg]</th>
        <th>Reps</th>
        <th>Pause in [s]</th>
        <th>Options</th>
    </tr>
    <tr *ngFor="let data of readData">
        <div *ngIf="isEdited == data.MPID;then editMode else showMode"></div>
        <form [formGroup]="musclepartForm" (ngSubmit)="musclepartUpdateSubmit()">

            <ng-template #editMode>
                <td>
                    <input class="edit_td" formControlName="musclepart" type="text" placeholder="{{data.name}}">
                </td>
                <td>
                    <input class="edit_td" formControlName="weight" type="text" placeholder="{{data.weight}}">
                </td>
                <td>
                    <input class="edit_td" formControlName="reps" type="text" placeholder="{{data.reps}}">
                </td>
                <td>
                    <input class="edit_td" formControlName="pause" type="text" placeholder="{{data.pause}}">
                </td>
                <td class="td_options">
                    <div>
                        <button type="submit" mat-icon-button color="primary" aria-label="Accept">
                            <mat-icon>check</mat-icon>
                        </button>
                        <button (click)="isEdited=false" mat-icon-button color="warn" aria-label="Cancel">
                            <mat-icon>cancel</mat-icon>
                        </button>
                    </div>
                </td>
            </ng-template>
        </form>

        <ng-template #showMode>
            <td class="td_name" (click)="gotoExercize(data.MPID, data.name)">{{data.name}}</td>
            <td>{{data.weight}}</td>
            <td>{{data.reps}}</td>
            <td>{{data.pause}}</td>
            <td class="td_options">
                <div>
                    <button (click)="editMusclepart(data.MPID)" mat-icon-button color="primary" aria-label="Edit muscle grouü">
                        <mat-icon>edit</mat-icon>
                    </button>  
                    <button (click)="deleteMusclepart(data.MPID, data.name)" mat-icon-button color="warn" aria-label="Add muscle part">
                        <mat-icon >delete</mat-icon>
                    </button>
                </div>
            </td>
        </ng-template>

    </tr>
</table>

There seems to be a conceptual misunderstanding in your form.您的表格似乎存在概念上的误解。 Reactive forms use a model-driven approach . Reactive forms 使用模型驱动的方法 This means that there should be an underlying model of all the displayed data that is editable.这意味着所有显示的数据都应该有一个底层 model 是可编辑的。 As all the rows in your table are editable, your form should represent this.由于表格中的所有行都是可编辑的,因此您的表单应该代表这一点。 In essence, your entire table should be one big form and the edit and submit buttons should basically toggle the visibility of the form rows, while saving the data to your form.本质上,你的整个表格应该是一个大表格,编辑和提交按钮应该基本上切换表格行的可见性,同时将数据保存到你的表格中。 You can use your readData to create a form to handle all the interactions in your table.您可以使用readData创建一个表单来处理表中的所有交互。

I created this stack blitz to help show what that might look like.我创建了这个堆栈闪电战来帮助展示它可能是什么样子。 The code isn't perfect, but hopefully you can adapt it to your needs.该代码并不完美,但希望您可以根据需要对其进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM