0

I am very new to AngularJS. I have read several posts on this topic, but am still unable to get data from external JSON file.

here is my code

data.json

[
    {
        "id": 1,
        "date": "20th August 2016",
        "day": "Sat",
        "in1": "10:30",
        "out1": "02:30",
        "in2": "04:00",
        "out2": "08:00",
        "in3": "",
        "out3": "",
        "total_hours": "8:00",
        "status": "1"
    },
    {   
        "id": 2,
        "date": "20th August 2016",
        "day": "Sat",
        "in1": "10:30",
        "out1": "02:30",
        "in2": "04:00",
        "out2": "08:00",
        "in3": "",
        "out3": "",
        "total_hours": "8:00",
        "status": "1"
    }
];

html

    <table ng-table="tctrl.tableEdit" class="table table-striped table-vmiddle">
       <tr ng-repeat="w in $data"  ng-class="{ 'active': w.$edit }">
          <td data-title="'ID'">
             <span ng-if="!w.$edit">{{ w.id }}</span>
             <div ng-if="w.$edit"><input class="form-control" type="text" ng-model="w.id" /></div>
          </td>
          ...
       </tr>                        
   </table> 

controller.js

.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) {

     var data = tableService.data;

     //Editable
     this.tableEdit = new ngTableParams({
        page: 1,            // show first page
        count: 10           // count per page
     }, {
        total: data.length, // length of data
        getData: function($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
     });

})

service.js

.service('tableService', [function($http){
        this.data = function() {
            return $http.get('data/timesheet.json');
        }
}])

I'm just trying to pull data from json file but it's not working and getting blank table. I don't know what is the issue. What I will get in this.data?

2
  • You are not invoking your function properly, try replacing this line var data = tableService.data; with this var data = tableService.data(); Commented Oct 1, 2016 at 16:36
  • It's not working @svarog Commented Oct 6, 2016 at 5:48

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.