0

I'm new to AngularJS and JS based web development, and I'm trying to make my portfolio site as a single page AngularJS application. I want to put all of my previous project data in a JSON file and use JS to grab the data and display it dynamically in an HTML list or table. I've tried doing a lot of research on this, but it's not working. My code is posted below.

myApp.controller('portController', function($scope, $http){
    
    $scope.test = "TEST PHRASE";
    
    $http.get('../projects.json').success(function(data) {
       $scope.projects = data;
   });

});
<div class="jumbotron text-center">
 <h1>Portfolio Home</h1>
    
    <p>{{ test }}</p>
    
    <table>
        <tr ng-repeat="project in projects">
            <td>{{project.title}}</td>
            <td>{{project.date}}</td>
        </tr>
    </table>
    
</div>
[
{
    "title" : "Sample Title", 
    "date" : "01/01/2001"
},
{
    "title" : "Sample Title 2", 
    "date" : "02/02/2002"
}
]

NOTE: I have a JS file that controls what is displayed on the page based on the URL (it handles all routing), and I am using ng-view and changing the controller to use the html above when on a certain page. The {{test}} bit worked fine until I added in the http request.

1 Answers1

0

To achieve expected use below option

  1. Change .success to .then , as .success is valid upto to Angularjs 1.4

  2. To render with ng-repeat, use $scope.projects = response.data , as shown below

    var myApp = angular.module('test',[]);
    
    myApp.controller('portController', function($scope, $http){
            $http({
                method: 'get', 
                url: '../projects.json'
            }).then(function (response) {
                $scope.projects= response.data;
            },function (error){
                console.log(error);
            });
    });
Naga Sai A
  • 10,771
  • 1
  • 21
  • 40