将数据从 Angular 8 个应用程序传递到 Laravel 后端以进行编辑

Passing data from Angular 8 app to Laravel backend for editing purpose

我正在开发一个在前端使用 Angular 8 并在后端使用 Laravel 构建的 CRUD 应用程序。前端是一个 SPA,在 CRUD 应用程序的更新功能上捕获编辑表单上的更新数据,通过服务将其解析到后端。

问题是在更新表单上的数据后,当我转储到 Laravel 中的后端控制器时,我得到空值。我在后端使用 put 方法

Edit.component.html 我更新了通过 ngModel

在每个输入字段上动态填充的数据
<form #editForm=ngForm (ngSubmit)="onSubmit()">

<div class="card-header childheader">Update Details</div>
    <div class="card-body">
         <div class="form-group row">
         <div class="col-sm-12">
            <label for="childFirstName">Child Name</label>
            <input 
                type="text" 
                name="childName" 
                class="form-control" 
                [ngModel]="singleUser?.name"
                id="childName" 
                placeholder="Child FirstName">
        </div>
    </div>
    <div class="form-group row">
         <div class="col-sm-6">
             <label for="childAge">Child Age</label>
            <select 
                class="form-control" 
                id="chAge" 
                name="childAge"
                [ngModel]="singleUser?.age" 
                required>
                <option value="" selected disabled> Child's Age</option>
                <option value="1"> 1 </option>
                <option value="2"> 2 </option>
                <option value="3"> 3 </option>
            </select>
        </div>
        <div class="col-sm-6">
            <label for="childGender">Child Gender</label>
            <select 
                class="form-control" 
                id="childGender" 
                name="childGender" 
                [ngModel]="singleUser?.gender" 
                required>
            <option value="" style="display:none"> Child's Gender</option>
                <option value="Male"> Male</option>
                <option value="Female"> Female </option>
            </select>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-12">
            <button 
                type="submit" 
                class="btn btn-lg btn-success btn-block" 
                [disabled]="!editForm.valid">Update Details </button>
        </div>
    </div>
    </div>
</form>

Edit.component.ts 文件

import { Component, OnInit } from '@angular/core';
import { SharedService } from 'src/app/Services/shared.service';
import { AuthService } from 'src/app/Services/auth.service';

@Component({
  selector: 'app-edit',
  templateUrl: './edit.component.html',
  styleUrls: ['./edit.component.css']
})
export class EditComponent implements OnInit {

  public singleUser : any[];
  public error = null;

  public form = {
    childName: null,
    childAge: null,
    childGender: null
  };

  constructor(
    private Shared : SharedService,
    private Auth:AuthService,
  ) { }

  //Pass the data to the backend via a common service
  onSubmit(){
    this.Auth.editedData(this.form).subscribe(
      data => console.log(data),
      error => console.log(error)
    );
  }

  ngOnInit() {
     //Pass the data to be edited to the view
     this.Shared.edit$.subscribe(message => this.singleUser = message);
  }

}

授权服务

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})

export class AuthService {

  private baseUrl = 'http://localhost/Laravel-anngular-spa/backend/public/api';

  constructor(private http:HttpClient) {}

  //Pass data to the backend
  editedData(data:any){
    return this.http.post(`${this.baseUrl}/editedData` , data);
  }
}

Laravel后端路由

   Route::put('editedData', 'SubmitFormController@updateData');

Laravel 控制器

  public function updateData(Request $request){
        dd($request->all());
    }

如果你想获取表单数据,你需要这样做。

先在模板中用ngForm注册变量,然后在ngsubmit中传入数据

<form (ngSubmit)="onSubmit(registerForm)" #registerForm="ngForm">
    <div class="form-group">
      <label for="UserName">Your email</label>
      <input
        type="text"
        ngModel
        class="form-control"
        id="UserName"
        name="UserName"
        required
      />
      <div
        *ngIf="
          registerForm.controls.UserName?.errors?.required &&
          registerForm.controls.UserName?.touched
        "
        class="alert alert-danger"
      >
        Email is required
      </div>
    </div>

    <div class="form-group">
      <label for="password">Your password</label>
      <input
        type="password"
        ngModel
        class="form-control"
        id="Password"
        name="Password"
        required
      />
      <div
        *ngIf="
          registerForm.controls.Password?.errors?.required &&
          registerForm.controls.Password?.touched
        "
        class="alert alert-danger"
      >
        Password is required
      </div>
    </div>

    <button
      type="submit"
      class="btn btn-success"
      [disabled]="!registerForm.form.valid"
    >
      Submit
    </button>
  </form>

然后在组件中访问值使用formValue.value.something

完整代码

onSubmit(formValue: NgForm) {
    const registerModel: AccountModel = {
      UserName: formValue.value.UserName,
      Password: formValue.value.Password
    };

    return this.authService
      .register(registerModel)
      .subscribe(
        res => this.toastr.success("Create account success"),
        error => this.toastr.error("Something went wrong")
      );
  }

由于您将 singleUser 用作表单值的 [(NgModel)](双向绑定),数据将呈现到 singleUser. 但是您正在尝试在 html.

形式的任何地方未使用的形式

使用 this.form 而不是 singleUser 您将能够更新您的 table 数据。

component.html

<form #editForm=ngForm (ngSubmit)="onSubmit()">

<div class="card-header childheader">Update Details</div>
    <div class="card-body">
         <div class="form-group row">
         <div class="col-sm-12">
            <label for="childFirstName">Child Name</label>
            <input 
                type="text" 
                name="childName" 
                class="form-control" 
                [ngModel]="form?.name"
                id="childName" 
                placeholder="Child FirstName">
        </div>
    </div>
    <div class="form-group row">
         <div class="col-sm-6">
             <label for="childAge">Child Age</label>
            <select 
                class="form-control" 
                id="chAge" 
                name="childAge"
                [ngModel]="form?.age" 
                required>
                <option value="" selected disabled> Child's Age</option>
                <option value="1"> 1 </option>
                <option value="2"> 2 </option>
                <option value="3"> 3 </option>
            </select>
        </div>
        <div class="col-sm-6">
            <label for="childGender">Child Gender</label>
            <select 
                class="form-control" 
                id="childGender" 
                name="childGender" 
                [ngModel]="form?.gender" 
                required>
            <option value="" style="display:none"> Child's Gender</option>
                <option value="Male"> Male</option>
                <option value="Female"> Female </option>
            </select>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-sm-12">
            <button 
                type="submit" 
                class="btn btn-lg btn-success btn-block" 
                [disabled]="!editForm.valid">Update Details </button>
        </div>
    </div>
    </div>
</form>

希望对您有所帮助..:)