0

I am getting some data with http and updating the $scope.comments variable.Then I display it with ng-repeat however variable is updating correctly but ng-repeat is not working. Here is javascript:

$scope.updateComments = function(taskId){
    var contentInput = document.getElementById(taskId);
    contentInput.value = '';
    $http.get('/loadTask/'+taskId).success(function(task){
        angular.forEach(task.comments,function(comment){
            $http.get('/loadComment/'+comment).success(function(c){
                $scope.comments[comment] = c;
                console.log('Comments now',$scope.comments);
            });
        });
    });
}

I can't use $scope.$apply because it's already being called by $http request. This is html:

   <div class="row">
            <div class="col-sm-12 col-md-12 col-lg-12">
                {{comments}}
                <div class="comments" ng-repeat="comment in task.comments track by $index">


                    <p><a href="{{comments[comment].author}}">
                        {{comments[comment].author}}    
                    </a>{{comments[comment].content}}</p>

                </div>
            </div>
        </div>

Also {{comments}} is updating correctly. Can anyone help me, thanks in advance!

7
  • in your ng-repeat, you looped task.comments which is not a property of $scope Commented Jan 8, 2016 at 23:46
  • @PrinceG but I am accesing comments which is part of scope Commented Jan 8, 2016 at 23:48
  • yeah, but task.comments is only accessible inside your success callback. Basically you are looping an undefined variable in your ng-repeat -> ng-repeat="comment in task.comments track by $index" Commented Jan 8, 2016 at 23:50
  • @PrinceG oooh thank you very mach! Commented Jan 8, 2016 at 23:53
  • 1
    as an aside...do you REALLY want to make a new $http request per comment? Commented Jan 9, 2016 at 2:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.