等到最后一个函数完成它的工作,然后在 angularjs 中
Wait till last function finish its work then in angularjs
我正在创建一个小应用程序,其中有 40 个小部件,我正在循环和获取数据并重新绘制它。
$scope.loopWiget = function(){
for(var i=0; i < $scope.totalWidget.length; i++)
{
if($scope.totalWidget[i].widgetType == "chart"){
$scope.fetchData($scope.totalWidget[i]);}
if($scope.totalWidget[i].widgetType == "table"){
$scope.fetchDataFortable($scope.totalWidget[i]);}
}
};
$scope.fetchData = function(id){
// fetching data and in response calling other function
$http({
method: 'POST',
url: 'rest/dataset/metadata',
type: 'json',
headers:{'id':id}
}).success(function(response){
$scope.drawWid(response);
})
};
$scope.fetchDataFortable= function(id){
// fetching data and in response calling other function
$http({
method: 'POST',
url: 'rest/dataset/metaTabledata',
type: 'json',
headers:{'id':id}
}).success(function(response){
$scope.drawWidTable(response);
})
};
$scope.drawWid = function(response){
// All draw logic comes here
};
$scope.drawWidTable= function(response){
// All draw logic comes here
};
$scope.totalWidget = [{'widgetId':'1','widgetType':'table'},{'widgetId':'2','widgetType':'chart'},{'widgetId':'3','widgetType':'maps'},{'widgetId':'4','widgetType':'chart'}];
$scope.loopWiget();
现在我的循环不会等待完成 drawWid 函数并再次调用 fetchdata,因此我在第一个小部件中获取第二个小部件的数据。 那么如何在循环中等待,直到 draw 函数完成其代码,然后为下一个小部件调用 fetchData 函数。
由于这是一个异步请求,因此随机呈现数据很常见。对此,可能的解决方案很少:
第一种方法:从响应中将数据存储在数组中,当所有数据都被获取后,只调用 draw 函数来逐个渲染小部件或在循环索引处渲染小部件。
widgets.forEach((widget, index) => {
return this.myService.getData(widget.entityProp)
.then(res => {
this.drawWid(res, index);
});
});
drawWid(res, index) {
compute logic accroding to the index of data .
}
第二种方法:您可以使用 $q.all() 从所有请求中获取数据,然后渲染它。
你能试试以下代码吗:
var index = 0;
$scope.loopWiget = function(){
if(!$scope.totalWidget[index]){
return;
}
var id = $scope.totalWidget[index]['widgetId'];
if($scope.totalWidget[index].widgetType == "chart"){
$scope.fetchData(id);
}
if($scope.totalWidget[index].widgetType == "table"){
$scope.fetchDataFortable(id);
}
};
$scope.fetchData = function(id){
$http({
method: 'POST',
url: 'rest/dataset/metadata',
type: 'json',
headers:{'id':id}
}).success(function(response){
$scope.drawWid(response);
index++;
});
}
$scope.drawWid = function(response){
// All draw logic comes here
$scope.loopWiget();
};
$scope.totalWidget = [{'widgetId':'1','widgetType':'table'},{'widgetId':'2','widgetType':'chart'},{'widgetId':'3','widgetType':'maps'},{'widgetId':'4','widgetType':'chart'}];
$scope.loopWiget();
相关文章:
- Javascript解析器本身是用类似ESPRIMA的Javascript编写的,这是如何工作的?然后谁解析ESPRIM
- JavaScript onclick在IE 9中工作两次,然后停止
- 为什么这个脚本只工作一次?只替换元素一次,然后再也不替换
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- 即首先破坏我的 JS 脚本,然后我按 F12,它工作得很好
- 铁:路由器工作很奇怪 - 将我发送到模板然后返回
- AJAX 查询工作 50+ 次,然后崩溃 JavaScript
- 需要单击以完全执行,然后才能再次单击工作
- Javascript - 我如何让一个语句工作一次,如果它已经工作了 1 次,则不能工作,然后再次工作
- 函数只在第二次单击时工作,然后工作正常
- 点击提交按钮禁用然后表单提交在firefox上工作,但不在chrome&狩猎
- ajax只工作一次,然后在单击后停止
- 需要选中取消选中,然后选中单选按钮才能工作
- 从html文本框中获取输入,然后将其放入Javascript变量中不工作
- HTML5游戏-工作在IE, Chrome.在FireFox中失败…直到我导航到一个.mp3文件…然后它才工作
- Javascript:承诺不工作,然后在解析完成之前执行
- PHP保存文本文件,然后在另一个页面上显示它并不是每次都工作
- 在jQuery中通过AJAX发送表单,然后保存在MySQL中,但不工作
- 网站不能在移动设备上工作,然后在0.2秒内恢复到谷歌搜索
- Node.js脚本工作一次,然后失败