数组中的Angular2 Typescript修剪文件扩展名 [英] Angular2 Typescript Trim File Extensions from array

查看:83
本文介绍了数组中的Angular2 Typescript修剪文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从Angular 2 TS上的数组中修剪文件扩展名.

I was wondering if it is possible to trim file extensions from an array on Angular 2 TS.

我知道在JS中进行修剪很容易,但是想知道在TS中进行修剪有多么容易.

I know it is easy to trim within JS, but wondered how easy it is to do within TS.

同样,我已返回此数据:

Lets same i have this data returned:

[ '1111111.pdf', '1111112.pdf' ]

我该如何从数组中修剪'.pdf'?

How would I approach to trim the '.pdf' from the array?

我知道在js中,您可以使用 String.prototype.trim.apply ,但是可以...任何建议都会被接受

I know in js you can use String.prototype.trim.apply but yano... any suggestions would be apprecaited

推荐答案

我将使用自定义管道.

工作演示: https://plnkr.co/edit/I45tLyFfXyjKIq5A7Mqu?p=预览

Working Demo: https://plnkr.co/edit/I45tLyFfXyjKIq5A7Mqu?p=preview

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'extension' })

export class RemoveExtension implements PipeTransform {
  transform(item) {
    return item.substring(0, item.indexOf('.'))
  }

}


import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `      
      <h1>Remove Extension:</h1>      
      <li *ngFor="let item of myData">{{item|extension}}</li>
  `
})
export class AppComponent {
 myData=[ 'abc.pdf', 'xyz.pdf' ];
}

这篇关于数组中的Angular2 Typescript修剪文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆