【问题标题】:How to set date-format with condition in angularjs?如何在angularjs中使用条件设置日期格式?
【发布时间】:2017-06-04 18:24:13
【问题描述】:

如果列类型日期,我想设置日期格式。

我的代码如下:

<div ng-repeat="col in gridColumnDefs">
    <div pi-grid-column sort-enable="true" sort-direction="0"
         filter-enable="true" column-width="185px"
         display-name="{{col.DisplayName}}"
         column-type="{{col.ColumnType.toLowerCase()"
         property-name="{{col.Name}}">
    </div>
</div>

如果我知道列类型,请编写如下代码:

<div pi-grid-column sort-enable="true" column-width="100px"
     sort-direction="0" display-name="Date 1" property-name="Date1"
     column-type="date" date-format="dd/MM/yyyy" filter-enable="true">
</div>

我使用 ng-repeat,gridColumnDef 是我的列数组。

display-name="{{col.DisplayName}}" : 'Date1'
 column-type="{{col.ColumnType.toLowerCase()" : "date"
property-name="{{col.Name}} : "date1"

if col.ColumnType = "date" ? date-format: dd/MM/yyyy 。我不知道如何在 div 标签内的 html 页面中进行操作

如果 columnType = date 则应设置 date-format,如果 columnType != date 未设置 date-format。

有什么想法吗?

【问题讨论】:

    标签: javascript html angularjs date


    【解决方案1】:

    您可以使用 Angular 表达式插入自定义属性日期格式所需的逻辑:

    <div pi-grid-column sort-enable="true" column-width="100px"
        sort-direction="0" display-name="Date 1" property-name="Date1"
        column-type="date" date-format="{{ col.ColumnType === 'date' ? 'date' : 'dd/MM/yyyy' }}" filter-enable="true">
    </div>
    

    我建议将此逻辑移动到控制器并调用一种方法来检索正确的值:

    $scope.defineDateFormat = function _defineDateFormat(column) {
        return column.ColumnType === 'date' ? 'date' : 'dd/MM/yyyy';
    }
    

    然后:

    <div pi-grid-column sort-enable="true" column-width="100px"
        sort-direction="0" display-name="Date 1" property-name="Date1"
        column-type="date" date-format="{{ defineDateFormat(col) }}" filter-enable="true">
    </div>
    

    【讨论】:

      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 2011-02-19
      • 2023-03-22
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      相关资源
      最近更新 更多