【问题标题】:My Angular 5 Pipe "trim" is not getting called, why?我的 Angular 5 Pipe“修剪”没有被调用,为什么?
【发布时间】:2018-05-01 15:12:49
【问题描述】:

我创建了一个名为trim 的角度Pipe。此管道旨在删除字符串中的最后一个字符。这是我的管道类TrimPipe。在 HTML 中使用管道时,控制台不会记录值。 这里的 HTML 用法 -

<ng-container *ngFor="let bg of backgrounds.preferred">
       <span>{{bg.name ? (bg.name + ', ') : '' | trim}}</span>
   </ng-container>

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

@Pipe({
  name: 'trim'
})
export class TrimPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    console.log(value, args, 'Pipes');
    return value.toString().substring(0, value.length - 1);
  }

}

我的app.module.ts 文件-

import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';

// Custom
import {AppComponent} from './app.component';
import {CommonService} from './shared/services/common.service';
import {DirectivesModule} from './shared/directives/directives.module';
import {PipeModule} from './shared/pipe/pipe.module';]


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    DirectivesModule,
    PipeModule,
    HttpClientModule,
    BrowserModule,
    BrowserAnimationsModule,
    NgSelectModule,
    FormsModule
  ],
  providers: [CommonService],
  bootstrap: [AppComponent]
})
export class AppModule {
}

我的pipe.module.ts -

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TrimPipe } from './trim.pipe';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [TrimPipe],
  exports: [TrimPipe]
})
export class PipeModule { }

【问题讨论】:

    标签: angular pipe angular-pipe


    【解决方案1】:

    当 bg.name 为假时,您仅在空字符串上使用管道。通过移动括号来修复:

    <span>{{(bg.name ? bg.name + ', ' : '') | trim}}</span>
    

    顺便说一句,如果您将整个逻辑移动到管道或在将字符串传递给模板(即在组件或服务代码中)之前预先格式化字符串,您将获得性能优势。 Angular 在每个变化检测周期都在模板插值中运行所有评估,而纯管道被缓存,直到输入值发生变化。

    【讨论】:

    • 现在我可以安然入睡了......非常感谢伙计......真是个愚蠢的错误......
    【解决方案2】:

    这是一个非常保守的版本,它应该能捕捉到任何错误。

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({name: 'trim'})
    export class TrimPipe implements PipeTransform {
      transform(inValue: any): string {
        let value = '';
        try {
          value = inValue.toString();
          console.log('TrimPipe: >>' + value + '<<');
          return value.substring(0, value.length - 1);
        } catch (err) {
          console.log(err);
          return value;
        }
      }
    }
    

    总体而言,您的代码看起来是在正确的位置,但我发现有几个问题:

    1. 转换应始终返回字符串
    2. 尝试/抓住内脏是个好主意,这样您就有机会看到任何错误。

    我发现对于将在随机位置使用的较低级别的东西,最好“防御性地”编写它们以在任何可能向它们投掷的东西中幸存下来。

    【讨论】:

    • Javascript 引擎会让你知道你知道的任何未捕获的错误:)
    • 是的,我遇到过它们出现在意想不到的地方的情况,并且很难通过 35 层闭包来追踪它们,所以我倾向于尝试在尽可能靠近的地方捕捉它们如果我觉得合适,它们就会发生。
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2022-10-15
    • 2018-12-15
    • 2014-02-03
    相关资源
    最近更新 更多