Angularjs循环trought $ http.post

编程入门 行业动态 更新时间:2024-10-12 01:26:45
本文介绍了Angularjs循环trought $ http.post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我通过$ HTTP POST服务Angularjs循环

When I loop through the $http post service for Angularjs

for (var i = 0; i < $scope.tagStyles.length; i++) { $scope.profilTag.tag = $scope.tagStyles[i].id_tag; $scope.profilTag.texte = $scope.tagStyles[i].style ; $scope.profilTag.profil = lastDocId; $http.post('/ajouterProfilTag',$scope.profilTag) .success(function(data){ if (data=='err'){ console.log("oops"); } }); };

我得到的只是最后的元素在我的数据库。有什么事情涉及到异步调用?

I get just the last element in my database. Is it something related to asynchronous call ?

推荐答案

的$ HTTP服务实际上不会发送,直到下一个$摘要的请求()被执行。

大概是什么情况是$ scope.profilTag正在通过引用传递至$ HTTP和只发送一个$消化后。您覆盖参考每次迭代,这就是为什么你只能用你的最后一个项目就走了。

What probably happens is that $scope.profilTag is being passed by reference to $http and only being sent after a $digest. You override that reference each iteration and that's why you only left with your last item.

请注意,功能具有范围但环不要!

Be aware that functions has scopes but for loops don't!

$scope.tagStyles.forEach(function(item){ var profilTag = { tag: item.id_tag, texte: item.style, profil: lastDocId, }; $http.post('/ajouterProfilTag',profilTag) .success(function(data) { if (data=='err'){ console.log("oops"); } }); });

更多推荐

Angularjs循环trought $ http.post

本文发布于:2023-10-10 09:46:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:trought   Angularjs   post   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!