AngularJS:service $ http.get JSON文件

时间:2016-09-30 14:19:47

标签: angular-ui-bootstrap

我对AngularJS很新。我已经阅读了有关此主题的几篇帖子,但我仍然无法从外部JSON文件中获取数据。

这是我的代码

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');
        }
}])

我只是试图从json文件中提取数据,但它不起作用并获得空白表。我不知道是什么问题。我将在this.data得到什么?

0 个答案:

没有答案