【发布时间】:2022-05-03 14:11:24
【问题描述】:
在我的 Angular 2 应用程序的构造函数中,我需要将返回数据的服务函数分配给一个公共变量并在 html 视图中显示。控制台日志运行良好,但没有显示 html 数据。所以我假设数据没有分配给变量。以下是我的代码 sn-p。
export class ListComponent implements OnInit {
public listname ="Laravel list";
constructor(private laraveldataservice: LaravelDataServiceService) { }
public dataList = this.laraveldataservice.getUsers();
ngOnInit() {
this.laraveldataservice.getUsers();
}
}
基本上 ng oninit 数据正在控制台日志中加载。但是 dataList 值没有赋值。
服务代码,
getUsers(){
const url = 'http://localhost/laravel_app/public/api/users';
this.http.get(url).subscribe(
res => {
const data = res.json();
console.log(data);
return data;
}
);
}
【问题讨论】:
-
你能贴出
laraveldataservice.getUsers()的代码吗? -
效果很好。由于控制台日志值分配正确。无论如何,我已经用代码更新了问题。
-
您是否在 html 页面中的
dataList上执行*ngFor?
标签: javascript angular