javascript - AngularJS 如何在类控制器中访问 axios 的结果?
问题描述
所以我有一个小型 AngularJS 1.5 应用程序,我想在其中使用 axios lib 和 ES6 类控制器将 JSON 文件中的一些数据放在视图中。
angular.module('app', []).controller('listController', listController)
控制器代码:
class CompanyListController {
constructor(){
this.list = ""
}
getList(){
axios.get("./company.list.json")
.then((response) => {
this.list = response.data;
console.log(response);
})
.catch((error) => {
console.log(error);
})
return list;
} }
看法:
<div ng-controller = "listController as list">
<div ng-bind = 'list.getList'></div>
<div>
{{ list.getList }}
</div>
</div>
视图中的 n-bind 给了我这个:
getList(){
return s.a.get("./company.list.json")
.then(e=>{this.list=e.data,console.log(e)})
.catch(e=>{console.log(e)}),this.list}
我猜这是 webpack 缩小的结果。并且 ng-bind 还
从 axios 执行 console.log,它完全记录了 axios 的结果。
{{ list.getList }}
什么都不扔。
axios和 类控制器语法必须在这个应用程序中。因此,Angular 确实将列表的值(null)放在了视图中,但在 axios 更改它之前。您能解释一下执行该操作的正确方法吗?
解决方案
axios 对服务器的异步请求。
class CompanyListController {
constructor(){
this.listJson = "";
this.getList();
}
getList(){
axios.get("./company.list.json")
.then((response) => {
this.listJson = response.data;
console.log(response);
})
.catch((error) => {
console.log(error);
});
}
}
html应该是这样的。
<div ng-controller = "listController as ctr">
<div ng-bind="ctr.listJson">
</div>
推荐阅读
- c# - 为什么比较每个 C# 字典键/值对会给出不同的结果,而不是一次比较字典键和值?
- .net - 如何获得数字的最低有效位
- java - 相当于 Python 线程守护模式的 Java 线程
- c# - 无法将会话用户 ID 转换为数据库
- javascript - 有什么方法可以通过浏览器控制我的电脑
- ruby - 如何在mongodb中运行继承类的方法?
- javascript - 是否有一种简单快捷的方法可以从 javascript 对象中删除 x 键而不循环键?
- php - 如何在php中的url中获取'@'-selector之后的所有内容
- javascript - 永久保存对 ContentEditable 所做的更改?
- java - 为什么在使用 LocalDate 作为 POJO 的数据类型时,univocity 的 CsvParser 会抛出错误以及如何解决它?