Unforeseeable behaviour $http in Angular JS



  • There's a code.

        $http.get('articles/articles.json').success(function(data){
        $scope.articles = data;
        angular.forEach(data, function(article) {
            $scope.article = [];
            var url = 'articles/'+ article.year +'/' + article.mounth + '/articles' + article.titlemounth + '.json';
            console.log(url);
            $http.get(url).success(function(data){  
                console.log(url);
                $scope.article.push(data);
                data = '';
            });
        });
    });
    

    This is the conclusion of the first log.

    articles/2016/april/articlesApril.json
    articles/2016/may/articlesMay.json
    articles/2016/jun/articlesJune.json
    

    Here's the second.

    articles/2016/april/articlesApril.json
    articles/2016/jun/articlesJune.json
    articles/2016/may/articlesMay.json
    

    That's not how it works. Please tell me what you can do with it.



  • Use forEach There is access to the current request number, so the answer should simply be based on the right index:

    $scope.articles = [];
    angular.forEach(data, function(article, index) {
        $scope.article = [];
        var url = 'articles/'+ article.year +'/' + article.mounth + '/articles' + article.titlemounth + '.json';
        console.log(url);
        $http.get(url).success(function(data){  
            console.log(url);
            $scope.article[index] = data;
        });
    });
    

    In doing so, the logs are also arbitrary, but the conclusion will be in the order in which settled.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2