{"id":13627248,"url":"https://github.com/ngneat/bind-query-params","last_synced_at":"2025-04-06T01:09:05.247Z","repository":{"id":46076676,"uuid":"323338476","full_name":"ngneat/bind-query-params","owner":"ngneat","description":"Sync URL Query Params with Angular Form Controls","archived":false,"fork":false,"pushed_at":"2023-06-20T18:28:04.000Z","size":1636,"stargazers_count":152,"open_issues_count":6,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-29T21:40:12.091Z","etag":null,"topics":["angular","forms","queryparams","synchronization"],"latest_commit_sha":null,"homepage":"https://netbasal.com","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ngneat.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":"ngneat"}},"created_at":"2020-12-21T13:04:55.000Z","updated_at":"2024-09-26T09:33:25.000Z","dependencies_parsed_at":"2022-09-02T11:01:23.816Z","dependency_job_id":null,"html_url":"https://github.com/ngneat/bind-query-params","commit_stats":{"total_commits":87,"total_committers":6,"mean_commits":14.5,"dds":0.6666666666666667,"last_synced_commit":"cbd30488452e3b01fb23c3840abf3750690f2ecb"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fbind-query-params","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fbind-query-params/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fbind-query-params/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngneat%2Fbind-query-params/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ngneat","download_url":"https://codeload.github.com/ngneat/bind-query-params/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247006667,"owners_count":20868033,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","forms","queryparams","synchronization"],"created_at":"2024-08-01T22:00:31.951Z","updated_at":"2025-04-06T01:09:05.227Z","avatar_url":"https://github.com/ngneat.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n \u003cimg width=\"20%\" height=\"20%\" src=\"./logo.svg\"\u003e\n\u003c/p\u003e\n\n\u003cbr /\u003e\n\n[![MIT](https://img.shields.io/packagist/l/doctrine/orm.svg?style=flat-square)]()\n[![commitizen](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)]()\n[![PRs](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)]()\n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n[![All Contributors](https://img.shields.io/badge/all_contributors-0-orange.svg?style=flat-square)](#contributors-)\n[![ngneat](https://img.shields.io/badge/@-ngneat-383636?style=flat-square\u0026labelColor=8f68d4)](https://github.com/ngneat/)\n[![spectator](https://img.shields.io/badge/tested%20with-spectator-2196F3.svg?style=flat-square)]()\n![CI](https://github.com/ngneat/bind-query-params/workflows/Bind%20query%20params/badge.svg?branch=master)\n\n\u003e Sync URL Query Params with Angular Form Controls\n\nThe library provides a simple and reusable solution for binding URL query params to Angular Forms\n\n## Demo\n\n\u003cimg src=\"./demo.gif\"\u003e\n\n## Installation\n\n`npm install @ngneat/bind-query-params`\n\n## Usage\n\nInject the `BindQueryParamsFactory` provider, pass an array of [definitions](#QueryParamDefinition) and `connect` it to your form:\n\n\u003c!-- prettier-ignore --\u003e\n```ts\nimport { BindQueryParamsFactory } from '@ngneat/bind-query-params';\n\ninterface Filters {\n  searchTerm: string;\n  someBoolean: boolean;\n}\n\n@Component({\n  template: `Your normal form setup`,\n})\nexport class MyComponent {\n  filters = new FormGroup({\n    searchTerm: new FormControl(),\n    someBoolean: new FormControl(false),\n  });\n\n  bindQueryParamsManager = this.factory\n    .create\u003cFilters\u003e([\n      { queryKey: 'searchTerm' },\n      { queryKey: 'someBoolean', type: 'boolean' }\n     ]).connect(this.filters);\n\n  constructor(private factory: BindQueryParamsFactory) {}\n\n  ngOnDestroy() {\n    this.bindQueryParamsManager.destroy();\n  }\n}\n```\n\nWith this setup, the `manager` will take care of two things:\n\n1. Update the `control`'s value when the page is loaded for the first time\n2. Update the URL query parameter when the corresponding `control` value changes\n\n## QueryParam Definition\n\n### `queryKey`\n\nThe query parameter key\n\n### `path`\n\nThe form control path. If it is not specified, the manager assumes that the `path` is the `queryKey`. We can also pass nested keys, for example, `person.name`:\n\n```ts\n{ queryKey: 'name', path: 'person.name' }\n```\n\n### `type`\n\nSpecify the control value type. Available options are:\n`boolean`, `array`, `number`, `string` and `object`.\nBefore updating the control with the value, the manager will parse it based on the provided `type`.\n\n### `parser`\n\nProvide a custom parser. For example, the default `array` parser converts the value to an `array` of strings. If we need it to be an array of numbers, we can pass the following `parser`:\n\n```ts\nconst def = { parser: (value) =\u003e value.split(',').map((v) =\u003e +v) };\n```\n\n### `serializer`\n\nProvide a custom serializer. For example, supposing that we have a `FormControl` that carries a Date and we want to persist, in the query params, a custom value, such as a `string` Date, we can do something like the following `serializer`:\n\n```ts\nconst def = { serializer: (value) =\u003e (value instanceof Date ? value.toISOString().slice(0, 10) : (value as string)) };\n```\n\n\n### `syncInitialControlValue`\nSet the initial control value in the URL (defaults to `false`)\n\n### `syncInitialQueryParamValue`\nSync the initial query paramater with the form group (defaults to `true`)\n\n#### Handle Async Data\nWhen working with async controls, such as a dropdown list whose options are coming from the server, we cannot update the control immediately. In those cases, you can set `syncInitialQueryParamValue` to `false`, which will force the control value to not be updated when the page loads. \n\nOnce the data is available, we can call the `manager.syncDefs()` or `manager.syncAllDefs()` methods to update the controls based on the current query parameters:\n\n```ts\n@Component()\nexport class MyComponent {\n  filters = new FormGroup({\n    searchTerm: new FormControl(),\n    users: new FormControl([]),\n    someBoolean: new FormControl(false),\n  });\n\n  bindQueryParamsManager = this.factory\n    .create\u003cFilters\u003e([\n      { queryKey: 'searchTerm' },\n      { queryKey: 'someBoolean', type: 'boolean' },\n      { queryKey: 'users', type: 'array', syncInitialQueryParamValue: false },\n    ])\n    .connect(this.filters);\n\n  constructor(private factory: BindQueryParamsFactory) {}\n\n  ngOnInit() {\n    service.getUsers().subscribe((users) =\u003e {\n      // Initalize the dropdown\n      this.users = users;\n      // Sync specific controls use:\n      this.manager.syncDefs('users');\n      // Sync all controls\n      this.manager.syncAllDefs();\n    });\n  }\n\n  ngOnDestroy() {\n    this.bindQueryParamsManager.destroy();\n  }\n}\n```\n\nNote that `syncDefs` will always be called once under the hood.\n\n### Form Changes\n\nIn case the form changes after the bindQueryParamsManager's `connect` was called you'll need to call the `reconnect` method to subscribe to the new form.\n\n```ts\n@Component()\nexport class MyComponent {\n  filters = new FormGroup({\n    searchTerm: new FormControl(),\n    users: new FormControl([]),\n    someBoolean: new FormControl(false),\n  });\n\n  bindQueryParamsManager = this.factory\n    .create\u003cFilters\u003e([\n      { queryKey: 'searchTerm' },\n    ])\n    .connect(this.filters);\n\n  constructor(private factory: BindQueryParamsFactory) {}\n\n  ngOnInit() {\n   this.filters.setControl('searchTerm', new FormControl());\n   // We need to reconnect the manager due to the replacement of the control\n   this.bindQueryParamsManager.reconnect(this.filters);\n  }\n\n  ngOnDestroy() {\n    this.bindQueryParamsManager.destroy();\n  }\n}\n```\n\n## Browser Support\n\nThe library uses the [URLSearchParams](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) API, which supported in any browser except IE.\n","funding_links":["https://github.com/sponsors/ngneat"],"categories":["Projects by main language"],"sub_categories":["angular"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngneat%2Fbind-query-params","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fngneat%2Fbind-query-params","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngneat%2Fbind-query-params/lists"}