本文介绍了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
发布评论