开发手册 欢迎您!
软件开发者资料库

Angular 2 - 转换数据

Angular 2转换数据 - 从简单和简单的步骤学习Angular 2,从基本到高级概念,包括概述,环境,Hello World,模块,架构,组件,模板,指令,元数据,数据绑定,使用HTTP的CRUD操作,错误处理,路由,导航,表单,CLI,依赖注入,高级配置,第三方控件,数据显示,处理事件,转换数据,自定义管道,用户输入,生命周期挂钩,嵌套容器,服务。

Angular 2有很多可用于转换数据的过滤器和管道.

小写

这用于转换输入全部小写.

语法

Propertyvalue | lowercase

参数

结果

属性值将转换为小写.

示例

首先确保app.component中存在以下代码. ts文件.

import {    Component } from '@angular/core'; @Component ({    selector: 'my-app',    templateUrl: 'app/app.component.html' }) export class AppComponent {    TutorialName: string = 'Angular JS2';    appList: string[] = ["Binding", "Display", "Services"]; }

接下来,确保app/app.component.html文件中存在以下代码.

    The name of this Tutorial is {{TutorialName}}
     The first Topic is {{appList[0] | lowercase}}
    The second Topic is {{appList[1] | lowercase}}
    The third Topic is {{appList[2]| lowercase}}
 

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

小写

大写

这用于将输入转换为全部大写.

语法

Propertyvalue | uppercase

参数

无.

结果

属性值将转换为大写.

示例

首先确保app.component中存在以下代码.ts文件.

import {    Component } from '@angular/core';@Component ({    selector: 'my-app',    templateUrl: 'app/app.component.html' }) export class AppComponent {    TutorialName: string = 'Angular JS2';    appList: string[] = ["Binding", "Display", "Services"]; }

接下来,确保app/app.component.html文件中存在以下代码.

    The name of this Tutorial is {{TutorialName}}
     The first Topic is {{appList[0] | uppercase }}
    The second Topic is {{appList[1] | uppercase }}
    The third Topic is {{appList[2]| uppercase }}
 

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

大写

切片

这用于从输入字符串中对一段数据进行切片.

语法

Propertyvalue | slice:start:end

参数

  • 开始 : 这是切片应该从哪里开始的起始位置.

  • 结束 : 这是切片应该结束的起始位置.

结果

属性值将根据开始和结束位置进行切片.

示例

首先确保app.component.ts文件中存在以下代码

import {   Component} from '@angular/core';@Component ({   selector: 'my-app',   templateUrl: 'app/app.component.html'})export class AppComponent {   TutorialName: string = 'Angular JS2';   appList: string[] = ["Binding", "Display", "Services"];}

接下来,确保app/app.component.html文件中存在以下代码.

    The name of this Tutorial is {{TutorialName}}
     The first Topic is {{appList[0] | slice:1:2}}
    The second Topic is {{appList[1] | slice:1:3}}
    The third Topic is {{appList[2]| slice:2:3}}
 

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

Slice

date

这用于将输入字符串转换为日期格式.

语法

Propertyvalue | date:"dateformat"

参数

dateformat : 这是输入字符串应转换为的日期格式.

结果

属性值将转换为日期格式.

示例

首先确保app.component.ts文件中存在以下代码.

import {    Component } from '@angular/core';  @Component ({    selector: 'my-app',    templateUrl: 'app/app.component.html' }) export class AppComponent {    newdate = new Date(2016, 3, 15); }

接下来,确保app/app.component.html文件中存在以下代码.

    The date of this Tutorial is {{newdate | date:"MM/dd/yy"}}
  

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

日期

currency

这用于将输入字符串转换为货币格式.

语法

Propertyvalue | currency

参数

无.

结果

属性值将转换为货币格式.

示例

首先确保应用程序中存在以下代码. component.ts文件.

import {    Component } from '@angular/core';  @Component ({    selector: 'my-app',    templateUrl: 'app/app.component.html' }) export class AppComponent {    newValue: number = 123; }

接下来,确保app/app.component.html文件中存在以下代码.

    The currency of this Tutorial is {{newValue | currency}}
      

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

货币

百分比

这用于将输入字符串转换为百分比格式.

语法

 Propertyvalue | percent

参数

结果

属性值将转换为百分比格式.

示例

首先确保app.component中存在以下代码.ts文件.

import {    Component } from '@angular/core';  @Component ({    selector: 'my-app',    templateUrl: 'app/app.component.html' }) export class AppComponent {    newValue: number = 30; }

接下来,确保app/app.component.html文件中存在以下代码.

   The percentage is {{newValue | percent}}
 

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

Percentage

管道百分比的另一种变化如下.

语法

Propertyvalue | percent: ‘{minIntegerDigits}.{minFractionDigits}{maxFractionDigits}’

参数

  • minIntegerDigits : 这是整数位数的最小数量.

  • minFractionDigits : 这是小数位数的最小数量.

  • maxFractionDigits : 这是小数位数的最大数量.

结果

属性值将被转换百分比格式

示例

首先确保app.component.ts文件中存在以下代码.


来自'@ angular/core'的

import {    Component } from '@angular/core';  @Component ({    selector: 'my-app',    templateUrl: 'app/app.component.html' }) export class AppComponent {    newValue: number = 0.3; }

接下来,确保app/app.component.html文件中存在以下代码.

    The percentage is {{newValue | percent:'2.2-5'}}
  

输出

保存所有代码更改并刷新浏览器后,您将获得以下输出.

Percent Pipe