angular2-pipe

Custom pipe to sort array of array

为君一笑 提交于 2019-12-31 07:03:47
问题 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> 回答1: It's not a good idea to use a pipe for sorting. See the link here: https:/

Custom pipe to sort array of array

荒凉一梦 提交于 2019-12-31 07:03:21
问题 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> 回答1: It's not a good idea to use a pipe for sorting. See the link here: https:/

angular2 pipe not working

非 Y 不嫁゛ 提交于 2019-12-23 04:56:30
问题 I am trying to have search functionality in angular2. So far i have created my own custom pipe for this like below: search.pipe.ts import { Pipe, PipeTransform ,Injectable} from '@angular/core'; @Pipe({ name: 'search', pure: false }) @Injectable() export class SearchPipe implements PipeTransform { transform(components: any[], args: any): any { var val = args[0]; if (val !== undefined) { var lowerEnabled = args.length > 1 ? args[1] : false; // filter components array, components which match

How to tell angular (2) currency pipe to display as it is if value is a string not int or float

走远了吗. 提交于 2019-12-21 17:25:53
问题 The currency pipe should be smart enough to handle string , float , int , etc automatically. if passed value is string or not int or float , it should do nothing and display the passed value as it is. And only display formatted value if it is int or float . It was happening in angularJs but not happening in angular (2) How to tell currency pipe to escape in case its string and do currency formatting if its a decimal value. I am expecting something like below. Example <div>Money:{{'xxx/vv/cc'

How to re-trigger all pure pipes on all component tree in Angular 2

大憨熊 提交于 2019-12-21 03:24:28
问题 I have pure pipe TranslatePipe that translates phrases using LocaleService that has locale$: Observable<string> current locale. I also have ChangeDetectionStrategy.OnPush enabled for all my components including AppComponent . Now, how can I reload whole application when someone changes language? (emits new value in locale$ observable). Currently, I'm using location.reload() after user switches between languages. And that's annoying, because whole page is reloaded. How can I do this angular

Split string based on spaces and read that in angular2

帅比萌擦擦* 提交于 2019-12-20 19:35:14
问题 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? 回答1: 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

Dynamically change locale for DatePipe in Angular 2

独自空忆成欢 提交于 2019-12-20 09:18:23
问题 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? 回答1: 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

Angular 2: Using pipes with ngModel

ぐ巨炮叔叔 提交于 2019-12-18 04:12:49
问题 I was using JQuery inputmask in one of my forms along with [(ngModel)] , but for some reason they won't work together. Using either one by itself works perfectly fine, but combining the two completely breaks [(ngModel)] and new inputs don't get sent back to the component. After struggling with this for a while I figured using Angular 2's pipes would be a good solution, however I can't figure out how to get those two to work together either. Here is some code I am using for testing my pipe

invoke pipe during run time using pipe name /metadata

半腔热情 提交于 2019-12-17 18:59:21
问题 I'm trying to build a dynamic table where i wand to decide in run time which pipe to use (If Any). I'm trying to achieve something similar to (Simplified): export class CellModel { public content: any; public pipe: string } Table <tbody> <tr *ngFor="let row of data"> <template ngFor let-cell [ngForOf]=row> <td *ngIf="cell.pipe">{{cell.content | cell.pipe}}</td> <td *ngIf="!cell.pipe">{{cell.content}}</td> </tr> </tbody> I understand that this example gives an error. Can i use Reflect is some

Angular 2 - Import pipe locally

心不动则不痛 提交于 2019-12-12 19:25:19
问题 THE SITUATION: I need to use a pipe in only one component. For this reason i didn't wanted to import it globally but only in the component. I have tried looking for reference on how to do it but couldn't find it. This is my attempt: THE PIPE: when tested globally is working fine import { Pipe, PipeTransform } from '@angular/core'; @Pipe({name: 'keys'}) export class KeysPipe implements PipeTransform { transform(value, args:string[]) : any { let keys = []; for (let key in value) { keys.push(