我使用Angular和Laravel 5.5创建了一个小型CRUD应用程序。在应用程序中,我有三个单选按钮,当我尝试获取它们的值时,出现错误...
类型错误,表明它无法读取数据类型
下面是我脚本的相关部分...
HTML
<div class="form-group row">
<label f class="col-sm-5 col-form-label col-form-label-sm">Can you get over unsound mind?
<span class="req">*</span>
</label>
<label class="col-sm-1 col-form-label col-form-label-sm">
<input type="radio" name="unsound" [(ngModel)]="userDetails.isUnsoundMind" [value]="false"/>
<span> No</span>
</label>
<label class="col-sm-1 col-form-label col-form-label-sm">
<input type="radio" name="unsound" [(ngModel)]="userDetails.isUnsoundMind" [value]="true"/>
<span> Yes</span>
</label>
</div>
TypeScript
constructor(public data: DataService,private userService: UserService, private sanitizer: DomSanitizer, private route: ActivatedRoute, private router: Router, private iNcoreService: IncorporationService, private spinner: NgxSpinnerService, private httpClient: HttpClient) {}
userDataSubmit() {
const data = {
....
isUnsoundMind: this.userDetails['isUnsoundMind'],
....
};
console.log(data);
this.userService.userDataSubmit(data)
.subscribe(
req => {
console.log("successful");
},
error => {
console.log(error);
}
);
模型
export interface IUserData {
...
isUnsoundMind: string;
...
}
服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { APIConnection } from './connections/APIConnection';
import { Observable } from 'rxjs';
import { IUserData } from '../models/user.model';
@Injectable({
providedIn: 'root'
})
export class UserService {
url: APIConnection = new APIConnection();
constructor(private router: Router, private http: HttpClient) { }
userDataSubmit(data: IUserData): Observable<IUserData> {
return this.http.post<IUserData>(this.url.getUserDataSubmit(), data);
}
}
控制器
<?php
namespace App\Http\Controllers\API\v1\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Address;
use App\User;
use Storage;
use App;
use URL;
use App\Http\Helper\_helper;
class useritorController extends Controller
{
use _helper;
public function saveuserData(Request $request)
{
$userAddress = new Address();
$userAddress->address1 = $request->input('localAddress1');
$userinfo = new user();
$userinfo->is_unsound_mind = $request->input('isUnsoundMind');
$userinfo->save();
}
}
就我而言,上述所有脚本中都没有这样的错误,但是每次我尝试将数据添加到数据库中时,都会出现上述错误,有人说,当我们添加单选按钮时,大部分要用TS脚本编写的代码,但是没有论坛对此提供适当的解释。有人可以帮我这个忙,或者就如何使用Angular 6 + Laravel 5.5向数据库表中添加单选按钮数据提供适当的指导吗?
答案 0 :(得分:0)
在打字稿代码中,您已经定义了数据。尝试按以下方式定义数据,并查看其是否有效:
const data:IUserData = {
isUnsoundMind: this.userDetails['isUnsoundMind']
};