Angular 如何自定义 pipe 管道以及参数传递问题
Angular 如何 以及 自定义 管道 参数传递 pipe 问题
2023-09-14 09:04:00 时间
下图第11行代码的 replace,是我自定义的 pipe 在Component 模板文件中的调用之处。标号1和2为其传入的参数,通过冒号进行参数传递。
其中 wordStartPattern 为 replace pipe 的第一个参数,这是一个 Component 属性:
第二个传入 pipe 的参数为 $&,硬编码。
而 pipe 接受的原始值,即 | 之前的值,这个值默认会始终传入 pipe.
完整的实现代码:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'replace',
})
export class ReplacePipe implements PipeTransform {
transform(
value: string,
searchValue: string | RegExp,
replaceValue: string,
): string {
const result = value.replace(searchValue, replaceValue);
console.log(`Jerry own pipe, original value: ${value},
search value: ${searchValue}, replaceValue: ${replaceValue}, result: ${result}`);
return result;
}
}
更多Jerry的原创文章,尽在:“汪子熙”:
相关文章
- [Angular] Using Pipe for function memoization
- [Angular] Create a custom pipe
- [AngularJS] Angular 1.3 ngAria - 2
- [Angular 8 Unit Testing] Testing a dump component
- [Angular] How to show global loading spinner for application between page navigation
- [Angular 2] NgNonBindable
- SAP Spartacus angular.json 中定义的 serve-ssr
- Angular页面发生更新时,更新如何从view层传递到model层
- Angular页面在开发模式(DevMode)下的ng-reflect-属性是如何生成的
- Angular DefaultDomRenderer2.setProperty - HTML的值是如何从Angular Component flow过来的,以及跨平台支持
- atitit.React 优缺点 相比angular react是最靠谱的web ui组件化方案了
- 如何处理 Angular 单页面应用里的 a 标签,避免点击后重新加载整个应用
- Angular 如何根据一个 class 的定义和数据,动态创建一个该类的实例
- Angular Injector.create的工作原理
- fixture.detectChange如何通过Angular zone执行其异步逻辑的
- Angular jasmine如何从detectChange触发refreshView进而执行到Component的hook实现
- Angular如何响应DOM event
- Angular @NgModule providers里multi等于true在源代码里如何体现的
- Angular DefaultDomRenderer2.setProperty - HTML的值是如何从Angular Component flow过来的,以及跨平台支持
- Angular自定义structural指令的实例化过程以及set方法的调用
- Angular页面在开发模式(DevMode)下的ng-reflect-属性是如何生成的
- Angular如何自定义attribute指令
- Angular html property的值如何被更新的
- Angular应用里child Component如何向parent Component发送事件
- 使用Angular HTTP client对数据模型进行创建操作
- 如何使用Angular的@Input()装饰器