ng repeat not displaying content

Viewed 89

I got a basic problem, though I don't figure out, why it's not working:

<article id="desktop">
    <h3>Content: </h3>
    <ul>
        <li ng-repeat="x in storage">
            name: {{x.name}}
        </li>
    </ul>
</article>

And my angular:

$scope.storage = [];    
...

$scope.showDesktop = function(){        
$http.get("/getDesktop").then(function(response){
    for(var i = 0; i<response.data.length; i++){
        $scope.storage.push({
            name: response.data[i]
        });
    }
    console.log($scope.storage);
    return;
});        
}

There must be an syntax error but I can't figure out where it is. I alredy looked in the documentation and copied the ng-repeat syntax, but it still doesn't work.

Console.log shows just the correct content.

My Spring controller looks like:

    //Returns Desktop
@GetMapping("/getDesktop")
public ArrayList<String> getDesktop() throws Exception {
    ArrayList<String> itemNames = new ArrayList<>();

    if(kdxF.getLogged() && kdxF.getUser() != null) {
        itemNames = kdxF.showDesktop();
    }else {
        throw new Exception("Not logged in!");
    }   

    return itemNames;
}
1 Answers

You need to scope the variable in order to access it from the html

var storage = []; //It is a private declaration and html has no access to it

Convert it with $scope.storage = []; and the ng-repeat will have access to it then.

Also don't forget to change the variable in api too

$http.get("/getDesktop").then(function(response){
    for(var i = 0; i<response.data.length; i++){
        $scope.storage.push({
            name: response.data[i]
        });
    }
    console.log($scope.storage);
    return;
}, function (err){
    //log the error callback
    console.log(err);
});  

Edited: One possible issue could be either due to ng-repeat tracking sequence. For that we use ng-repeat="x in storage track by $index"

Be insure that you are logging the error response in api calling.

Related