angular2-pipe

How to mock Pipe when testing Component

蹲街弑〆低调 提交于 2019-12-04 08:50:22
问题 Currently I am overriding providers to use mocked services like this: beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { tcb.overrideProviders(AddFieldToObjectDropdownComponent, [ provide(ServiceA, { useClass: MockServiceA })), provide(ServiceB, { useClass: MockServiceB })) ])... I want to do same thing for pipes that the component uses. I tried, provide(PipeA, { useClass: MockPipeA }) and provide(PipeA, { useValue: new MockPipeA() }) but both didn't work. 回答1: You can

Get length of array in ngFor after pipes transformation

此生再无相见时 提交于 2019-12-04 01:39:42
I have the following template: <div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = length"> Here is the length of my ngFor : {{l}} </div> Unfortunately length doesn't exist in ngFor. How can I work around this issue to have the length available inside my ngFor? Another solution could be the following <div *ngFor="let item of myArray | customPipe1 | customPipe2; let l = count"> Here is the length of my ngFor : {{l}} </div> Plunker Example See also https://github.com/angular/angular/blob/master/packages/common/src/directives/ng_for_of.ts#L15-L17 <div *ngFor="let item of myArray

Angular 2 - Pipe reuse in multiple modules - error not found or duplicate definition

半世苍凉 提交于 2019-12-03 18:49:16
问题 Im working on angular 2 final release. I have declared two modules : main app and one for the settings page . The main module is declaring globally pipes. This module is also including the settings module . app.module.ts @NgModule({ imports: [BrowserModule, HttpModule, routing, FormsModule, SettingsModule], declarations: [AppComponent, JsonStringifyPipe], bootstrap: [AppComponent] }) export class AppModule { } settings.module.ts @NgModule({ imports: [CommonModule, HttpModule, FormsModule,

Split string based on spaces and read that in angular2

試著忘記壹切 提交于 2019-12-03 05:32:22
I am creating a pipe in angular2 where I want to split the string on white spaces and later on read it as an array. let stringToSplit = "abc def ghi"; StringToSplit.split(" "); console.log(stringToSplit[0]); When I log this, I always get "a" as output. Where I am going wrong? Made a few changes: let stringToSplit = "abc def ghi"; let x = stringToSplit.split(" "); console.log(x[0]); The split method returns an array. Instead of using its result, you are getting the first element of the original string. let stringToSplit = "abc def ghi"; StringToSplit.split(" "); console.log(stringToSplit[0]);

How to mock Pipe when testing Component

风流意气都作罢 提交于 2019-12-03 02:06:55
Currently I am overriding providers to use mocked services like this: beforeEach(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { tcb.overrideProviders(AddFieldToObjectDropdownComponent, [ provide(ServiceA, { useClass: MockServiceA })), provide(ServiceB, { useClass: MockServiceB })) ])... I want to do same thing for pipes that the component uses. I tried, provide(PipeA, { useClass: MockPipeA }) and provide(PipeA, { useValue: new MockPipeA() }) but both didn't work. You can add your mockpipes in the declarations of the TestBed : TestBed.configureTestingModule({ declarations: [

Custom Pipe | filter for calculating relative time in angular2

 ̄綄美尐妖づ 提交于 2019-12-02 18:34:50
问题 During the learning process, I came across Creation of Custom Pipe , so I thought this will help. 回答1: Below is the code for custom pipe. import{PipeTransform,Pipe} from '@angular/core'; @Pipe({ name:'relativeTime' }) export class RelativeTimeFilterPipe implements PipeTransform{ transform(inputDate:string):string{ var current = new Date().valueOf(); var input = new Date(parseInt(inputDate)).valueOf(); var msPerMinute = 60 * 1000; var msPerHour = msPerMinute * 60; var msPerDay = msPerHour * 24

Dynamically change locale for DatePipe in Angular 2

柔情痞子 提交于 2019-12-02 18:25:45
I'm making an Angular project where the user has the ability to switch languages. Is it possible to make the locale dynamic? I have seen that you can add it in the NgModule but i'm guessing it's not dynamic when i put it there? Or can i change it somehow through a service or something? Using providers you can change your default locale in your NgModule . to do this You need to import LOCALE_ID from angular/core and fetch your locale language to pass the same to providers. import { LOCALE_ID } from '@angular/core'; @NgModule({ imports: [//your imports], providers: [ { provide: LOCALE_ID,

Custom pipe to sort array of array

浪子不回头ぞ 提交于 2019-12-02 10:24:00
I have a array of array having two elements each, i.e. arr[a[2]] . Index 0 is name and index 1 is size . I want a pipe to sort the array of array according to size ie index 1 . Example: arr [ [ 'hello' , '1' ] , [ 'how' , '5' ] , [ 'you' , '12' ] , [ 'are' , '6' ] ] Output of pipe should be : arr [ [ 'hello' , '1' ] , [ 'how' , '5' ] , [ 'are' , '6' ] , [ 'you' , '12' ] ] HTML file: <p> {{items | custompipe }}</p> It's not a good idea to use a pipe for sorting. See the link here: https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe Rather, add code in your component to perform

Register service in root AppModule or root AppComponent

不想你离开。 提交于 2019-12-01 12:38:58
问题 Everywhere, it is recommended to register a service in root AppModule providers array and avoid using providers array of root AppComponent. When should someone register a service in root AppComponent? Any practical example. What is the advantage of registering service in root AppModule compared to root AppComponent? 回答1: When registered at the root, the provided service is created as a singleton, as opposite to providing in the component, it will be created as many instances as you use the

Decimal Pipe in Angular 2 - Commas Only, No Decimal Places

让人想犯罪 __ 提交于 2019-11-30 12:43:58
I currently use this pipe {{ product.productPrice | number:'.2-2' }} And result is 1,000,000.00 but I want to remove the .00 How do you do that? Use this : {{product.productPrice | number: '1.0-0'}} 1.0-0 means: at least one digit before decimal point, 0 digits after decimal point. https://angular.io/api/common/DecimalPipe 来源: https://stackoverflow.com/questions/45955101/decimal-pipe-in-angular-2-commas-only-no-decimal-places