[Angular] Pipes as providers
In this example, we are going to see how to use Pipe as providers inject into component.
We have the pipe:
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'filesize'
})
export class FileSizePipe implements PipeTransform{
transform(value: number, ext: string = "MB") {
return (value / ( * )).toFixed() + ' ' + ext;
}
}
We want to inject this pipe as provider to the component:
import { Component, OnInit } from '@angular/core';
import {FileSizePipe} from './filesize.pipe';
interface File {
name: string,
size: number | string,
type: string
}
@Component({
selector: 'app-root',
template: `
<div>
<div *ngFor="let file of files">
<p>{{ file.name }}</p>
<p>{{ file.size | filesize: 'MB' }}</p>
</div>
<hr>
<div *ngFor="let file of mapped">
<p>{{ file.name }}</p>
<p>{{ file.size }}</p>
</div>
</div>
`,
providers: [
FileSizePipe
]
})
export class AppComponent implements OnInit {
files: File[];
mapped: File[];
constructor(
private fp: FileSizePipe
) {
}
ngOnInit() {
this.files = [
{ name: 'logo.svg', size: , type: 'image/svg' },
{ name: 'banner.jpg', size: , type: 'image/jpg' },
{ name: 'background.png', size: , type: 'image/png' }
];
this.mapped = this.files.map((file) => ({
name: file.name,
type: file.type,
size: this.fp.transform(Number(file.size), 'mb')
}));
}
}
As we can see, we use 'providers' keyword in the @Component:
providers: [
FileSizePipe
]
This enable us to inject pipe into component:
constructor(
private fp: FileSizePipe
) {
}
Then we just need to call transform method on the pipe:
this.mapped = this.files.map((file) => ({
name: file.name,
type: file.type,
size: this.fp.transform(Number(file.size), 'mb')
}));
In the html. we don't need to use '|filesize: 'MB'' anymore:
<div *ngFor="let file of mapped">
<p>{{ file.name }}</p>
<p>{{ file.size }}</p>
</div>
[Angular] Pipes as providers的更多相关文章
- [Angular 2] Value Providers & @Inject
Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...
- [Angular 2] BYPASSING PROVIDERS IN ANGULAR 2
Artical --> BYPASSING PROVIDERS IN ANGULAR 2 Here trying to solve one problem: On the left hand s ...
- [Angular] Difference between Providers and ViewProviders
For example we have a component: class TodoList { private todos: Todo[] = []; add(todo: Todo) {} rem ...
- Angular CurrencyPipe货币管道关于人民币符号¥的问题
做项目(Angular项目)时经常需要处理金额的显示,需要在金额前面加上¥,但又不想用简单在前面加"¥"这么不优雅的方式,于是想到了CurrencyPipe.毕竟,Currency ...
- @angular/cli项目构建--interceptor
JWTInterceptor import {Injectable} from '@angular/core'; import {HttpEvent, HttpHandler, HttpInterce ...
- Angular复习笔记6-依赖注入
Angular复习笔记6-依赖注入 依赖注入(DependencyInjection)是Angular实现重要功能的一种设计模式.一个大型应用的开发通常会涉及很多组件和服务,这些组件和服务之间有着错综 ...
- Angular 项目打包之后,部署到springboot项目中,刷新访问404解决方法
解决方法1: app.module.ts文件添加两行代码: import { LocationStrategy, HashLocationStrategy } from '@angular/commo ...
- Angular2 组件
1. 组件说明 Angular2 组件是构成Angular2应用程序的核心部分,Angualr2应用程序本质上来说就是一个组件树,Angular2组件一般由模块,注解,元数据以及组件类组成,实现组件类 ...
- Ionic2文档整理
来自:Rainey's Blog 原文地址:http://rainey.space/2016/04/06/Ionic2_Chinese_Document/ Github:https://github. ...
随机推荐
- 洛谷 P2628 冒险岛
P2628 冒险岛 题目背景 冒险岛是费老师新开发的一种情景模拟电脑的游戏,通过掷骰子(1~6个数字之间),让一种人物(棋子)在棋纸上从左至右的行走,从而模拟冒险的故事…… 题目描述 棋纸上有一条从左 ...
- method initializationerror not found:JUnit4单元測试报错问题
今天使用JUnit 4进行单元測试时,測试程序一直执行不起来,报method initializationerror not found错误.例如以下: 网上说版本 ...
- arcgis webapp builder 安装试用
ArcGIS WebApp Builder 是针对开发者的,用于高速构建基于HTML5/Javascript 技术的美观的 Web应用的一个工具. 用过Flex版本号的AppBuilder应该非常清楚 ...
- 谈谈Command对象与数据检索
摘要 到目前为止,我相信大家对于ADO.NET如何与外部数据源建立连接以及如何提高连接性能等相关知识已经牢固于心了.连接对象作为ADO.NET的主力先锋,为用户与数据库交互搭建了扎实的桥梁.它的一生是 ...
- 15、python学习手册之:列表和字典
1.列表属于可变序列,支持在原处的修改 2.在标准python解锁器内部,列表就是C数组而不是链接结构 3.内置函数map对序列中的各项应用一个函数并把结果收集到一个新的列表中 eg:list(map ...
- 【前端切图】用css画一个卡通形象-小猪佩奇
最近在腾讯云技术社区遇到了一位奇才,用css画出了一个社会人小猪佩奇,不得不服.研究了一下他的文章https://segmentfault.com/a/1190000014909658,感觉甚是有趣, ...
- 【Codeforces Round #431 (Div. 2) C】From Y to Y
[链接]点击打开链接 [题意] 让你构造一个大小最多为10W的字符multiset. 你进行n-1次操作; 每次操作,从set中取出两个字符串,一开始单个字符被认为是字符串. 然后把它们连接在一起. ...
- 洛谷——P1035 级数求和
https://www.luogu.org/problem/show?pid=1035 题目描述 已知:Sn= 1+1/2+1/3+…+1/n.显然对于任意一个整数K,当n足够大的时候,Sn大于K. ...
- java方法调用之动态调用多态(重写override)的实现原理——方法表(三)
上两篇篇博文讨论了java的重载(overload)与重写(override).静态分派与动态分派.这篇博文讨论下动态分派的实现方法,即多态override的实现原理. java方法调用之重载.重写的 ...
- JSP语法基础(一)
一.JSP页面中的凝视 (1)HTML凝视 <!-- comment [ <%=expression %> ] --> 能在client显示的一种凝视,标记内的全部JSP脚本元 ...