获取空 {} 而不是 id
getting empty {} instead of id
我正在尝试在 Angular 中执行更新操作。当我更新后提交时,它会在 API 末尾显示随机数,而不是特定的 ID 号。
请求URL是这样的
请求URL:http://localhost:4200/api/auth/role/%7B%7D/
应该是http://localhost:4200/api/auth/role/1/
service.ts
edit_role(param:any):Observable<any>{
let body = JSON.stringify(param);
console.log(body);
var headers = new HttpHeaders().set('Authorization', 'Token ' + localStorage.getItem('usertoken'));
var options = {
headers: headers
};
return this.httpClient.put('api/auth/role/' + body, options)
.map(success => success)
.catch(this.handleErrorObservable);
}
控制台显示空 {}
component.ts
editRole(id){
let editrole: any = {};
editrole['name'] = this.Editrole.name;
console.log(editrole);
let roleid:number;
roleid = this.Editrole.id;
this.Authentication.edit_role(editrole).subscribe(res => {
console.log('edited succesfully');
});
}
控制台显示{name:undefined}
Html
<form> <div class="form-group" *ngIf="roles">
<label for="text">Role:</label>
<input type="text" class="form-control"[(ngModel)]="roles.name" id="text" name="role" #role>
</div>
<button type="submit" class="btn btn-primary" (click)="editRole(role)">Submit</button> </form>
在组件中你应该像这样改变它
//class variable
editrole: any;
//in constructor
this.editrole={};
//in function
this.editrole={
name: this.roles.name,
id: this.roles.id
}
this.Authentication.edit_role(this.editrole).subscribe(res => {
console.log('edited succesfully');
});
在服务文件中
let body = param;
return this.httpClient.put('api/auth/role/' + body.id, body ,options)
我正在尝试在 Angular 中执行更新操作。当我更新后提交时,它会在 API 末尾显示随机数,而不是特定的 ID 号。 请求URL是这样的
请求URL:http://localhost:4200/api/auth/role/%7B%7D/
应该是http://localhost:4200/api/auth/role/1/
service.ts
edit_role(param:any):Observable<any>{
let body = JSON.stringify(param);
console.log(body);
var headers = new HttpHeaders().set('Authorization', 'Token ' + localStorage.getItem('usertoken'));
var options = {
headers: headers
};
return this.httpClient.put('api/auth/role/' + body, options)
.map(success => success)
.catch(this.handleErrorObservable);
}
控制台显示空 {}
component.ts
editRole(id){
let editrole: any = {};
editrole['name'] = this.Editrole.name;
console.log(editrole);
let roleid:number;
roleid = this.Editrole.id;
this.Authentication.edit_role(editrole).subscribe(res => {
console.log('edited succesfully');
});
}
控制台显示{name:undefined}
Html
<form> <div class="form-group" *ngIf="roles">
<label for="text">Role:</label>
<input type="text" class="form-control"[(ngModel)]="roles.name" id="text" name="role" #role>
</div>
<button type="submit" class="btn btn-primary" (click)="editRole(role)">Submit</button> </form>
在组件中你应该像这样改变它
//class variable
editrole: any;
//in constructor
this.editrole={};
//in function
this.editrole={
name: this.roles.name,
id: this.roles.id
}
this.Authentication.edit_role(this.editrole).subscribe(res => {
console.log('edited succesfully');
});
在服务文件中
let body = param;
return this.httpClient.put('api/auth/role/' + body.id, body ,options)