Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Sync URL Query Params with Angular Form Controls

License

NotificationsYou must be signed in to change notification settings

rafaelss95/bind-query-params

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


MITcommitizenPRsstyled with prettierAll ContributorsngneatspectatorCI

Sync URL Query Params with Angular Form Controls

The library provides a simple and reusable solution for binding URL query params to Angular Forms

Demo

Installation

npm install @ngneat/bind-query-params

Usage

Inject theBindQueryParamsFactory provider, pass an array ofdefinitions andconnect it to your form:

import{BindQueryParamsFactory}from'@ngneat/bind-query-params';interfaceFilters{searchTerm:string;someBoolean:boolean;}@Component({template:`Your normal form setup`,})exportclassMyComponent{filters=newFormGroup({searchTerm:newFormControl(),someBoolean:newFormControl(false),});bindQueryParamsManager=this.factory.create<Filters>([{queryKey:'searchTerm'},{queryKey:'someBoolean',type:'boolean'}]).connect(this.filters);constructor(privatefactory:BindQueryParamsFactory){}ngOnDestroy(){this.bindQueryParamsManager.destroy();}}

With this setup, themanager will take care of two things:

  1. Update thecontrol's value when the page is loaded for the first time
  2. Update the URL query parameter when the correspondingcontrol value changes

QueryParam Definition

queryKey

The query parameter key

path

The form control path. If it is not specified, the manager assumes that thepath is thequeryKey. We can also pass nested keys, for example,person.name:

{queryKey:'name',path:'person.name'}

type

Specify the control value type. Available options are:boolean,array,number,string andobject.Before updating the control with the value, the manager will parse it based on the providedtype.

parser

Provide a custom parser. For example, the defaultarray parser converts the value to anarray of strings. If we need it to be an array of numbers, we can pass the followingparser:

constdef={parser:(value)=>value.split(',').map((v)=>+v)};

serializer

Provide a custom serializer. For example, supposing that we have aFormControl that carries a Date and we want to persist, in the query params, a custom value, such as astring Date, we can do something like the followingserializer:

constdef={serializer:(value)=>(valueinstanceofDate ?value.toISOString().slice(0,10) :(valueasstring))};

strategy

When working with async control values, for example, a dropdown list that its options come from the server, we cannot immediately update the control.

In this cases, we can provide themodelToUrl strategy, that will not update the control value when the page loads. When the data is available we can call themanager.syncDefs() method that'll update the controls based on the current query params:

@Component()exportclassMyComponent{filters=newFormGroup({searchTerm:newFormControl(),users:newFormControl([]),someBoolean:newFormControl(false),});bindQueryParamsManager=this.factory.create<Filters>([{queryKey:'searchTerm'},{queryKey:'someBoolean',type:'boolean'},{queryKey:'users',type:'array',strategy:'modelToUrl'},]).connect(this.filters);constructor(privatefactory:BindQueryParamsFactory){}ngOnInit(){service.getUsers().subscribe((users)=>{// Initalize the dropdownthis.users=users;this.manager.syncDefs('users');});}ngOnDestroy(){this.bindQueryParamsManager.destroy();}}

Note thatsyncDefs will always be called once under the hood.

Browser Support

The library uses theURLSearchParams API, which supported in any browser except IE.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Netanel Basal

💻🖋📖🤔🚇

This project follows theall-contributors specification. Contributions of any kind welcome!

Icons made byFreepik fromwww.flaticon.com

About

Sync URL Query Params with Angular Form Controls

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript82.6%
  • JavaScript14.6%
  • HTML2.6%
  • SCSS0.2%

[8]ページ先頭

©2009-2025 Movatter.jp