3

我的 angularjs 控制器中有以下控制器

.controller('taskCtrl', ['$scope', function ($scope) {
    $scope.getRef = function () {
       return getTask();
    };

    $scope.save = function () {
          $.extend(true, getTask(), $scope.data);
    };
}])

和我的模板:

<tr>
  <td>
    <label class="ngTemplateTitle" > create Date</label>
  </td>
  <td>
    <input class="form-control" id="data.creationDate" type="text"ng-model="data.creationDate"/>
  </td>
</tr>

我需要以形式将公历日期转换并查看回历。然后在用户修改对象的值后将回历转换为公历日期。

4

1 回答 1

1

你熟悉moment.js吗?

您可以将其导入您的项目(非常轻的文件),并实现一个角度过滤器以按照您想要的方式解析它......

就像是:

angular.module('myFilters').filter('formatDateExample', function() {
var defaultFormat = "l";
return function(timestamp, format) {
    format = format || defaultFormat;
    return timestamp ? moment(new Date(timestamp)).format(format) : null;
    // when you'll call the filter from the HTML, it will parse it to gregorian date
};

});

在 html 模板中:

<div>{{myDateVar | formatDateExample }}</div>

或直接从 HTML 中使用自定义格式:

<div>{{myDateVar | formatDateExample: "DD/MM/YYYY, HH:MM" }}</div>

这个想法是以相同的方式将您的日期保存在模型中,并且仅当您想在视图中显示它时,才使用过滤器对其进行解析。

于 2014-03-05T08:47:19.120 回答