0

I created a json file under json folder json/caseNames.json and trying to read that file in a controller. Here is my code. When I debug this code it is not going to the next statement after $http.get line. How do I read the file? Does it have any syntax errors?

app.controller('CaseNamesController',function($http,$scope){
    $scope.names = [];

  $http.get('/json/caseNames.json')
    .success(function(response){
        console.log(response);
        $scope.names = response;
    })
    .error(function(response){
        $scope.names =[];
  });

});

2 Answers 2

1

success and error both are now deprecated, you should use then instead which receives the success handler as the first parameter, and the error handler as the second:

app.controller('CaseNamesController',function($http,$scope){
    $scope.names = [];
    $http.get('/json/caseNames.json').then(
        function(response){
            console.log(response);
            $scope.names = response;
        }, 
        function(response){
            $scope.names =[];
        }
    );
});
Sign up to request clarification or add additional context in comments.

3 Comments

I changed my code when I was trying to display in html page it is not displaying. here is my code.<div ng-controller="CaseNamesController as case">
<ul ng-repeat="names in case.names()><li>{{names.firstname}},{{names.lastname}}but nothing displaying.I am not sure where the issue is.
Try using $scope.$apply() after this assignment $scope.names = response
0

Add a ".." so that it can navigate to the json folder. I ran it using another JSON file and I was able to output the data on the console.

app.controller('CaseNamesController',function($http,$scope){ 

    $scope.names = [];

  $http.get("../json/caseNames.json")
    .success(function(response){
        console.log(response);
        $scope.names = response;
    })
    .error(function(response){
        $scope.names =[];
  });

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.