我有一个数据列表,我想从中选择一个对象并将其添加到另一个对象中:
这是第一个模型:
export class missionnaire {
cin : String ;
graade :grade;
constructor(cin? : String ,graade ?:grade) {
this.graade= new grade() ;
};
}
这是另一个模型:
export class grade {
constructor(public code? : String ,
public liba? : String ,
public libfr? :String ) {}
}
在“ missionaire.html”中,我有:
<form [formGroup]="missionnaireF">
<div class="form-group col-md-12">
<label> grade </label>
<input type = "text" class="form-control" id="search3" name="search3" list="list3" formControlName="graade" >
<datalist id="list3">
<option *ngFor="let grade of grades" data-value="grade" [value]="grade.liba">
</datalist>
</div>
<div class="form-group col-md-12">
<label >cin </label>
<input type="text" class="form-control" formControlName="cin" name="cin" maxlength="8">
</div>
</form>
这是json post请求,在后面工作正常:
{
"cin": 88663344,
"grade": {
"idgrade": 1,
"libGradeAr": "grade",
"libGradeFr": "grade",
"codeGrade": 1
}
}
答案 0 :(得分:0)
interface Grade {
idgrade?: number;
libGradeAr?: string;
libGradeFr?: string;
codeGrade?: number;
}
class Missionnaire {
grade: Grade;
constructor(grade?: Grade, public cin?: number) {
this.grade = grade;
}
}
let obj = {
"cin": 88663344,
"grade": {
"idgrade": 1,
"libGradeAr": "grade",
"libGradeFr": "grade",
"codeGrade": 1
}
}
let grd: Grade = obj.grade;
console.log(grd)
let x = new Missionnaire(grd, obj.cin);
console.log(x)
我假设这就是您想要做的。您可以将“等级”更改为界面,而不是“类别”(您可以将“等级”用作基本类别,将“传教士”用作超级类别)。其余的很简单。