Angular Can';t显示特定项的api中的Json
我想按ID显示特定的学生。这是它返回给我的错误:找不到类型为“object”的不同支持对象“[object object]”。我已经成功地显示了一个完整的列表,但当我尝试使用单个列表时,就会出现这种情况 我的组件Angular Can';t显示特定项的api中的Json,angular,Angular,我想按ID显示特定的学生。这是它返回给我的错误:找不到类型为“object”的不同支持对象“[object object]”。我已经成功地显示了一个完整的列表,但当我尝试使用单个列表时,就会出现这种情况 我的组件 export class StudentIDComponent implements OnInit { id: any; url: any; studentID; constructor( private route: ActivatedRoute, p
export class StudentIDComponent implements OnInit {
id: any;
url: any;
studentID;
constructor(
private route: ActivatedRoute,
private http: HttpClient,
private sqlService: StudentServerListService
) { }
getStudentID() {
return this.http.get("https://localhost:44303/api/student/" + this.route.snapshot.params['studentId']);
}
ngOnInit() {
this.id = console.log(this.route.snapshot.params['studentId']);
//this.studentID = this.sqlService.getOne();
this.studentID = this.getStudentID();
this.url = console.log("https://localhost:44303/api/student/" + this.route.snapshot.params['studentId']);
}
}
我的HTML
<h3>Student by ID</h3>
<div *ngFor="let key of studentID | async">
<h2>
{{key.studentId}}. xcxcx
{{key.studentName}}
</h2>
</div>
<h1>sdsdssd</h1>
学生身份证
{{key.studentId}。xCx
{{key.studentName}
SDSSD
我的示例Json
{
“主体”:[],
“学生ID”:3,
“studentName”:“Long”}
看起来您有一个对象,而不是数组。因此:
{{key.studentId}。xCx
{{key.studentName}
您应该订阅,而不是使用| async 关于组件
subscribeStudentID() {
this.http.get("https://localhost:44303/api/student/" + this.route.snapshot.params['studentId'])
.subscribe((data: any) => {
this.studentID = data;
});
}
ngOnInit() {
this.id = console.log(this.route.snapshot.params['studentId']);
//this.studentID = this.sqlService.getOne();
this.subscribeStudentID();
this.url = console.log("https://localhost:44303/api/student/" + this.route.snapshot.params['studentId']);
}
关于HTML
<h3>Student by ID</h3>
<div *ngIf="studentID">
<h2>
{{studentID.studentId}}. xcxcx
{{studentID.studentName}}
</h2>
</div>
<h1>sdsdssd</h1>
学生身份证
{{studentID.studentID}。xCx
{{studentID.studentName}
SDSSD
还有其他的解决方案,但对我来说这是最干净的,你也可以尝试使用studentID的get方法。你能用.html
{{studentID | async | json}
打印出来并在这里显示输出吗?如果你想显示一条记录,那就不需要了。直接显示它,因为你的json有对象值。@Mridul我也认为这对我来说很难,因为互联网上的Angular大部分都是列表之类的东西。你能给我发一个类似例子的链接吗?它不会告诉我细节,而是一些随机点,我特意放在上面,以确定有多少东西会被显示:
<h3>Student by ID</h3>
<div *ngIf="studentID">
<h2>
{{studentID.studentId}}. xcxcx
{{studentID.studentName}}
</h2>
</div>
<h1>sdsdssd</h1>