以前没有用ES6的时候,在controller中直接调用service异步取得数据,然后页面就能加载出来了。在按照ES6标准写之后,按照之前的方式调用,虽然数据能得到,但是不会刷新页面。网上的解决办法是在service中声明一个对象,然后在路由resolve中先请求数据保存到对象中,在controller中请求对象。以前的请求方式为什么不能用了呢?求大神给科普一下。
ES5:
controller.js
service.getData().then(function(res){
$scope.data = res;//异步请求数据,数据回来的时候页面能自动刷新
});
ES6:
service.js
var temp={};
getData(){
return temp;
};
getDataFromServer(){
RestService.get().then(res => {
temp = res;
});
}
router.js
//多余的不写了,直接写resolve
resolve:{
item:(service) => {
return service.getDataFromServer();
}
}
controller.js
$scope.data = service.getData();
ES6写法中,不能用ES5的调用方式,不在service中创建临时对象存数据,直接在controller中异步调用service,数据能在控制台输出,但是页面不会刷新。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
没看到代码,不确定具体问题。数据得到后$scope.$apply()不知道能不能帮到你
你断点在$scope.data = service.getData();有数据么?
你这个service的getDataFromServer()函数没有return promise。会不会是这个出的问题。你使用promise模式试试。