0

So my file.json is :

[
    { 
        'id' : 1,
        'name' : 'img/1.jpg' 
    },
    {
        'id' : 2,
        'name' : 'img/2.jpg' 
    },
    {
        'id' : 3,
        'name' : 'img/3.jpg' 
    },
    {
        'id' : 4,
        'name' : 'img/4.jpg' 
    },
    {
        'id' : 5,
        'name' : 'img/5.jpg' 
    }

]

And my controller is :

app.controller("MyCtrl", ["$scope", "$http", function($scope, $http){
    
    $http.get("file.json").then(function(data){
        $scope.template = data;
    
    });
    
}]);

So from my html page im calling the controller and doing a ng-repeat but not working:

<div class="container" ng-controller="MyCtrl">
<div ng-repeat="path in template.names">
<div class="inner">
    <img ng-src="{{path.name}}">
</div>
</div>

I keep getting error in console:

enter image description here

3 Answers 3

3

Your JSON is invalid, you need to use double quotes " rather than single quotes '

Also, because you are using then() the data you're after will be wrapped inside a response object, so you will have to update that part of your code as follows:

$http.get("file.json").then(function(response){
    $scope.names = response.data;
});

And then update your ng-repeat as @Anonymous suggests:

<div ng-repeat="path in names">

And change this part (the {{ }} are not necessary here):

<img ng-src="path.name">
Sign up to request clarification or add additional context in comments.

5 Comments

Followed this. <div ng-controller="MyCtrl" ng-repeat="path in names"> <p>{{path.name}}</p> </div> Even this is not working.
Hmm, strange, can you do a console.log(response) (inside of success callback) and post back what that returns?
Looks fine, so now you should be able to assign response.data to the $scope variable that you're using in your ng-repeat. Failing that can you do a {{ names }} in your view and see what that outputs.
I did the same: $http.get("file.json").then(function(response){ $scope.names = response.data; }); and in the html `<div ng-repeat="path in names"> <p> {{ path.names }} </p> </div>. But nothing is displayed.
1

It should be <div ng-repeat="path in template">, not <div ng-repeat="path in template.names">. And also json standard for string is double quotes.

2 Comments

I did that. Nothing is showing. I tried: <div class="container" ng-controller="MyCtrl"> <div ng-repeat="path in template"> <div class="inner"> <p>{{path.name}}</p> </div> </div> But nothing is showing. Nothing in the console also.
@Somename @Anonymous is correct in that you should change your ng-repeat, but you also need to change how you retrieve your data as it will be wrapped inside a response object.
0

try using this:

$scope.template = JSON.parse(data); 

And make sure your json file is valid: http://jsonlint.com/

Comments

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.