【问题标题】:AngularJS:How to read data from the json file using angularAngularJS:如何使用 Angular 从 json 文件中读取数据
【发布时间】:2014-10-14 06:25:43
【问题描述】:

我们正在使用JQuery ajax 读取json 数据并转换为对象。

jQuery 脚本

 $.ajax({
        type: "GET",
        dataType: 'json',
        url: 'data.json',
        converters:
    {
        "text json": function (data) {
            return $.parseJSON(data);
        }
    },
        success: function (data) {
            self.jsonData = data;
        }
    });

数据.json

[{
   "Name" : "Task 1",
   "ID" : 1,
   "StartTime" : "2014-02-02T00:00:00Z",
   "Effort" : "8:00:00",
   "Description" : "Description of Task 1"
 }, 
 {
   "Name" : "Task 2",
   "ID" : 2,
   "PredecessorIndices" : "1",
   "StartTime" : "2014-02-03T00:00:00Z",
   "Effort" : "16:00:00",
   "Description" : "Description of Task 2"
  }, 
  {
   "Name" : "Task 3",
   "ID" : 3,
   "StartTime" : "2014-02-02T00:00:00Z",
   "Effort" : "1.12:30:00",
   "ProgressPercent" : 90,
   "Description" : "Description of Task 3"      
 }]

这里将日期字符串转换为Jsdate对象,如下所示:Json Date Parsing

如何使用angularjs读取json数据并将json日期字符串解析成Jsdate对象?

【问题讨论】:

  • 你的问题不太清楚。
  • 我们需要在angular中解析json数据

标签: javascript json angularjs date date-conversion


【解决方案1】:

首先尝试not to mix JQuery with AngularJS

如果你想获取 json 文件,而不是 JQuery-Ajax 使用 AngularJS $http 服务如下所示,转换为日期对象可以使用Date.parse

 $scope.self = {};
  $http.get('data.json').success(function(data) {
    $scope.self.jsonData = data;
    $scope.self.jsonData.forEach(function(value, key) {
      value.StartTime = Date.parse(value.StartTime); // converting into date
    });
  });

在 HTML 中

<tr ng-repeat="arr in self.jsonData">
  <td>
      {{arr.StartTime |date: 'yyyy-MM-dd'}}
  </td>
</tr>

Working Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多