【问题标题】:How can I represent file size in a human readable way in Angular 6?如何在 Angular 6 中以人类可读的方式表示文件大小?
【发布时间】:2019-04-01 13:02:27
【问题描述】:

假设我在 html 中有 *ngFor="let cell of dateFormat(row)">{{cell | filesize}}</td>dateFormat 看起来是这样的

dateFormat(row:string){

    var today = new Date(row[4]);

    let latest_date = this.datepipe.transform(today, 'yyyy-MM-dd');

    row['4'] = latest_date;

    return row;

}

HTML:

<tr *ngFor="let row of this.UploaderService.tableData2.dataRows">
    <td [ngClass]="{'active': check(row) }" (click)="listClick($event, row)"
       (dblclick)="listDoubleClick($event, row)"
       *ngFor="let cell of dateFormat(row)">{{cell}}
    </td>
</tr>

有没有办法只在row['2'] 上调用filesize?目前filesize 可以正常工作,但它会格式化所有行。我试过dateFormat,但我不确定如何在打字稿中使用filesize。像这样的

let latest_date = this.datepipe.transform(today, 'yyyy-MM-dd');

row['4'] = latest_date;
row['2'] = here convert row['2'] using filesize;

return row;

有什么想法吗?

【问题讨论】:

  • 如果您使用的是一些网格系统或表格,您需要在您的问题中添加一些源代码。
  • @HDJEMAI 我添加了 HMTL 代码。你知道如何解决这个问题吗?

标签: angular html typescript angular6


【解决方案1】:
npm install ngx-filesize

在你的模块中:

import {NgxFilesizeModule} from 'ngx-filesize';



// ...

@NgModule({
    // ...
    imports: [
        // ...
        NgxFilesizeModule,
        // ...
    ]
    // ...
})

在您的组件中

{{bytes | filesize}}

*我知道这个解决方案是一个迟到的回复,但它可以帮助即将到来的开发人员:)

【讨论】:

    【解决方案2】:

    我没有你所有的代码,所以我创建了一个只有虚拟数据的示例,

    所以你可以这样做:

    *ngFor="let cell of dataRows">{{(row == 2)? (row | filesize): row }}
    

    如需演示,请参考this stackblitz

    在这种情况下,filesize 将仅在 row == 2 时应用

    【讨论】:

    • 但我需要显示单元格而不是行。此外,我需要像示例 *ngFor="let cell of dateFormat(row)"&gt;{{cell}} 中那样设置日期格式,一切正常,但我无法格式化文件大小。
    • 对不起,我不确定你的意思是什么?
    • 您必须提供您的单元格数据以及模板中使用的所有功能,以便有人帮助您
    • 在这个地方row['2'] = here convert row['2'] using filesize;可以在打字稿中使用filesize吗?
    猜你喜欢
    • 1970-01-01
    • 2013-02-17
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 2010-09-21
    相关资源
    最近更新 更多