[英]angularjs issue on loop
我正在嘗試調用一個名為chercherProfil的服務,並將其結果存儲到javascript數組$ scope.tagsByProfils中,但是當我嘗試訪問它時,控制台會將該數組顯示為未定義
這里是一段代碼,可以幫助您解決我的問題:
$http.post(configuration.URL_REQUEST + '/chercherProfil', toSendCherche)
.success(function(data) {
console.log(data);
$scope.profil = data;
$http.post(configuration.URL_REQUEST + '/chercherTagsParProfil', {
idProfil: $scope.target
}).success(function(data) {
$scope.tagsByProfils = data;
console.log(data);
$scope.tests = [];
console.log($scope.tagsByProfils[0].tag);
for (var p = 0; p < $scope.tagsByProfils.length; p++) {
$http.post(configuration.URL_REQUEST + '/getTagById', {
idTag: $scope.tagsByProfils[p].tag
}).success(function(data) {
$scope.resultFlag = data;
if ($scope.resultFlag.libelle.toUpperCase().match('^TITRE')) {
console.log($scope.tagsByProfils[p]);
$scope.tests[p] = '<p class="text-center" data-font="' + $scope.tagsByProfils[p].police + '" data-size="' + $scope.tagsByProfils[p].taille + '" data-lineheight="' + $scope.tagsByProfils[p].interligne + '" data-weight="' + $scope.tagsByProfils[p].interligne + '" data-coloration="' + $scope.tagsByProfils[p].coloration + '">' + $scope.resultFlag.libelle + ' : Ceci est un exemple de' + $scope.resultFlag.libelle + ' </p>';
} else {
$scope.tests[p] = '<p class="text-center" data-font="' + $scope.tagsByProfils[p].police + '" data-size="' + $scope.tagsByProfils[p].taille + '" data-lineheight="' + $scope.tagsByProfils[p].interligne + '" data-weight="' + $scope.tagsByProfils[p].interligne + '" data-coloration="' + $scope.tagsByProfils[p].coloration + '">' + $scope.resultFlag.libelle + ' : CnedAdapt est une application qui permet d\'adapter les documents. </p>';
}
});
};
});
任何想法或反饋都可以歡迎
不知道它是否可以完全解決您的問題,但是請考慮保護您的for循環計數器,因為您以封閉方式使用它:
for (var p = 0; p < $scope.tagsByProfils.length; p++) {
(function(p){
$http.post(configuration.URL_REQUEST + '/getTagById', {
idTag: $scope.tagsByProfils[p].tag
[...]
})(p)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.