Angular Custom Query Encoder

Every now and then I think about what I should post in this blog of mine. I keep meaning to post more about Angular since I use it almost everyday…

I’ve been working with Angular 2+ a lot for the past year, and it’s been some serious ups and downs.  Not just learning a new framework as a new developer, but the real pain has been upgrading between different versions! But that’s not what I’ll be talking about today.

I recently ran into the issue that Angular’s query encoder doesn’t encode all the characters that we need it to. For a while, we just used encodeURIComponent to encode our queries without using the Angular encoder or URLSearchParams. Today I’ve finally gotten around to extending the QueryEncoder and refactoring our code use URLSearchParams. It was actually really simple, and the code is much cleaner now! 😀


import { QueryEncoder } from '@angular/http';

export class CustomQueryEncoder extends QueryEncoder {

encodeValue(v: string): string {
return encodeURIComponent(v);
}
}

Leave a comment