Angular 自定义角度4管道不工作

Angular 自定义角度4管道不工作,angular,Angular,我用来缩短字符串的自定义管道不起作用。我已将其包含在app.module声明中,并将其导入到组件中。代码如下 ` import { Pipe, PipeTransform } from '@angular/core'; /* Takes a string and replaces it with a shortend version based on the length you give it if its greater than 14 char for Example som

我用来缩短字符串的自定义管道不起作用。我已将其包含在app.module声明中,并将其导入到组件中。代码如下

 `   import { Pipe, PipeTransform } from '@angular/core';
/*
  Takes a string and replaces it with a shortend version based on the length you give it if its greater than 14 char for Example
  someString = "hey whats up my name is Bob and im from Bob town"
  {{value | shortString : length of new string}}
  {{someString | shortString: 10}}
*/
@Pipe({name: 'shortString'})
export class shortString implements PipeTransform {
  transform(value: any, length: number): string {
    console.log('expected new string '+value.slice(0,length)+'...');
    return (value.length()>14)?value.slice(0,length)+'...': value;
  }
}`

管道本身看起来很好,但为了使用它,必须将其导入到模块中,并声明和导出它,以便它在组件中可用

import { shortString } from './shortString.pipe';

@NgModule({
    imports: [
    ],
    declarations: [
        shortString
    ],
    exports: [
        shortString
    ],
    providers: [
    ]

})
export class SharedModule { }

将此添加到适合您的设置的模块中,我将所有管道都放在SharedModule中,这就是为什么要导出SharedModule,但您可能在MainModule或其他模块中有此功能。

您的管道本身看起来不错,但为了使用它,您必须将其导入到您的模块中,并声明和导出它,以便它在组件中可用

import { shortString } from './shortString.pipe';

@NgModule({
    imports: [
    ],
    declarations: [
        shortString
    ],
    exports: [
        shortString
    ],
    providers: [
    ]

})
export class SharedModule { }
将此添加到适合您的设置的任何模块中,我将所有管道都放在SharedModule中,因此这就是为什么要导出SharedModule,但您可以将其放在MainModule或其他模块中。

首先放在管道中

@Pipe({name: 'shortString'})
export class shortString implements PipeTransform {
  transform(value: any, length: number): string {
    return (value.length()>14)?value.slice(0,length)+'...': value; // remove the length() it will be value.length
  }
}`
同一工作平台的工作平台

首先在你的管道中

@Pipe({name: 'shortString'})
export class shortString implements PipeTransform {
  transform(value: any, length: number): string {
    return (value.length()>14)?value.slice(0,length)+'...': value; // remove the length() it will be value.length
  }
}`
同一工作平台的工作平台


它怎么不起作用?它会产生错误吗?或者只是不做空价值?怎么会不起作用?它会产生错误吗?或者只是不缩短值?这很有效,我只需删除将.length()更改为.length。。。。谢谢C++中的思想是这样工作的,我必须删除.RethTh()到…长度。谢谢C++语言中的思维