【问题标题】:Display TimeSpan type Data in Angularjs Listing在 Angularjs 列表中显示 TimeSpan 类型数据
【发布时间】:2016-03-24 06:58:39
【问题描述】:

试图在表格中显示时间,但它以这种格式显示时间

{"Hours":16,"Minutes":8,"Seconds":45,"Milliseconds":0,"Ticks":581250000000,"Days":0,"TotalDays":0.6727430555555556,"TotalHours":16.145833333333332,"TotalMilliseconds":58125000,"TotalMinutes":968.75,"TotalSeconds":58125}

虽然我希望它以这种格式显示

"12:13:20"

在 Html 文件中我确实像这样列出

<table>
    <thead>
        <tr>
            <th>Start Time</th>
            <th>End Time</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in Festivals">
            <td>{{item.StartTime}}</td>
            <td>{{item.EndTime}}</td>
        </tr>
    </tbody>
</table>

我正在初始化数据的角度控制器代码

var app = angular.module('myApp');

app.controller('SeasonCtrl', [
    '$scope', '$http', 'seasonService', function ($scope, $http, seasonService) {

        $scope.Seasons = [];

        seasonService.GetAllSeasons = function () {
            seasonService.getSeasons().success(function (data) {
                $scope.Seasons = data.data;
            })
        }

        seasonService.GetAllSeasons();

    }
]);

C# 代码我正在向角度控制器发送数据

public JsonResult GetAllSeasons()
{
    var data = _seasonManager.AllSeasons();
    if (data != null)
    {
        var list = data.Select(r => new
        {
            r.StartTime,
            r.EndTime
        });

        return Json(new { data = list }, JsonRequestBehavior.AllowGet);
    }
    return null;
}

【问题讨论】:

  • 请添加您的代码。
  • 你能告诉我们item.StartTime的值吗?
  • 当我通过断点 {StartTime = {14:44:45}, EndTime = {14:44:45}} 检查对象时,此值来自数据库,而在浏览器中它以这种格式显示 { "小时":16,"分钟":8,"秒":45,"毫秒":0,"滴答声":581250000000,"天":0,"TotalDays":0.6727430555555556,"TotalHours":16.145833333333332,"TotalMilliseconds ":58125000,"TotalMinutes":968.75,"TotalSeconds":58125}

标签: angularjs timespan listings


【解决方案1】:

我刚刚尝试过,它解决了我的问题

在 HTML 中

<table>
    <thead>
        <tr>
            <th>Start Time</th>
            <th>End Time</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="item in Festivals">
            <td ng-bind='timeFunction(item.StartTime)'></td>
<td ng-bind='timeFunction(item.EndTime)'></td>
        </tr>
    </tbody>
</table>

在角度控制器中

$scope.timeFunction = function (timeObj) {
      var min = timeObj.Minutes < 10 ? "0" + timeObj.Minutes : timeObj.Minutes;
      var sec = timeObj.Seconds < 10 ? "0" + timeObj.Seconds : timeObj.Seconds;
      var hour = timeObj.Hours < 10 ? "0" + timeObj.Hours : timeObj.Hours;
      return hour + ':' + min + ':' + sec;
};

【讨论】:

    【解决方案2】:

    此解决方案使用过滤器和 Moment.js。

    在 JS 中:

    angular.module('app')
        .filter('showTimeSpan', function () {
            return function (timeObj, format) {
                return moment(timeObj, "HH:mm").format(format);
            };
        });
    

    在 HTML 中:

    <p>{{activity.startTime | showTimeSpan:"hh:mm a"}}</p>
    

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2015-06-14
      • 2023-03-10
      • 1970-01-01
      • 2018-01-17
      • 2010-11-12
      相关资源
      最近更新 更多