我的变量:
{{imageGrid.bannerName}}我的输出:
DINING_LANDING_PAGE_MEAL_PLAN_SUBSCRIBED_USER如何替换angularjs中的_?
发布于 2020-07-15 11:48:12
如果您使用的是Angular V2+,则可以编写自定义管道
import { Pipe, PipeTransform } from '@angular/core';
/*
* Replace the underscore with space
*/
@Pipe({name: 'underscore'})
export class UnderscorePipe implements PipeTransform {
transform(value: string): string {
return value.replace(/\_/g, ' ');
}
}此管道必须在模块中声明。即AppModule.ts
import { UnderscorePipe } from './underscore.pipe';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpClientModule
],
declarations: [
AppComponent,
UnderscorePipe
],
bootstrap: [AppComponent]
})
export class AppModule { }在HTML端
{{imageGrid.bannerName | underscore}}如果您想要一个更复杂的管道,我们可以传递参数
自定义管道实现
import { Pipe, PipeTransform } from '@angular/core';
/*
* Replace the the first paremeter with the second parameter
*/
@Pipe({name: 'replace'})
export class ReplacePipe implements PipeTransform {
transform(value: string, existing: string, latest: string): string {
return value.replace(new RegExp('\\'+existing, 'g'), latest);
}
}HTML文件
<h2>Hi Please {{value | replace: '_' : ' '}}</h2>https://stackoverflow.com/questions/62907111
复制相似问题