I am in problem with get api, here is student_name in api student. Here is my code I tried
Api
{
"error": null,
"result":
[{"id": 1, "student_name": "Alex" }, {"id":2, "student_name": "Bob"}]
}
Typescript: student.service.ts
listStudent(){
return this.http.get('api/student')
.map(HttpHandle.extractData.bind(null, 'list student'))
.catch(HttpHandle.handleError)
}
extractDate method in http-handle.ts:
public static extractData(name: string, res: Response): Observable<Response> {
if (res.status < 300) {
console.log(`%c# Response status ${res.status}, ${name}, ${res.arrayBuffer().byteLength} bytes`,
'color: #00CC00; font-weight:bold;');
if (printDetail) {
console.groupCollapsed('\tResponse');
console.log(`${JSON.stringify(res, null, 2)}`);
console.groupEnd();
}
}
else {
console.log(`%c# Response status ${res.status}, ${name}, ${res.arrayBuffer().byteLength} bytes`,
'color: #CC0000; font-weight:bold;');
console.groupCollapsed('\tResponse');
console.log(`${JSON.stringify(res, null, 2)}`);
console.groupEnd();
}
return res.json();
}
My Component: student.component.ts
export class StudentComponent{
lstStudent: string[];// I think something went wrong
public student_name: string;
....
constructor(
private studentService: StudentService,
) { }
....
getStudents(){
this.studentService.listStudent()
.subscribe(data =>{
this.lstStudent = data['lstStudent'];
this.student_name = this.lstStudent...;//This is my issue
}
);
}
Html
<select class="form-control" title="list Student"
[(ngModel)]="lstStudent" name="lstStudent">
<option *ngFor="let x of lstStudent" [ngValue]="x.student_name">{{x.student_name}}
</option>
</select>
So how to get student_name and use it for HTML in select tag.
Thank you.
jsonobject is incorrect it should be array[{"id": 1, "student_name": "Alex" }, {"id":2, "student_name": "Bob"}]for iterate inngFor.this.lstStudent?