在Angular 6中,如果无法将API数据绑定到客户端变量,可以尝试以下解决方法:
import { HttpClient } from '@angular/common/http';
constructor(private http: HttpClient) { }
getDataFromApi() {
this.http.get('https://api.example.com/data').subscribe((data: any) => {
this.clientVariable = data;
});
}
{{ clientVariable }}
ngOnInit() {
this.getDataFromApi();
}
确保替换https://api.example.com/data
为实际的API URL,并将clientVariable
替换为你的客户端变量名。
这样,当组件初始化时,它将从API获取数据并将其绑定到客户端变量,然后你可以在HTML模板中使用该变量来显示数据。