当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

bind-query-params

Sync URL Query Params with Angular Form Controls
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 通学真
操作系统 跨平台
开源组织
适用人群 未知
 软件概览


CI

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 the BindQueryParamsFactory provider, pass an array of definitions and connect it to your form:

import { BindQueryParamsFactory } from '@ngneat/bind-query-params';

interface Filters {
  searchTerm: string;
  someBoolean: boolean;
}

@Component({
  template: `Your normal form setup`,
})
export class MyComponent {
  filters = new FormGroup({
    searchTerm: new FormControl(),
    someBoolean: new FormControl(false),
  });

  bindQueryParamsManager = this.factory
    .create<Filters>([
      { queryKey: 'searchTerm' },
      { queryKey: 'someBoolean', type: 'boolean' }
     ]).connect(this.filters);

  constructor(private factory: BindQueryParamsFactory) {}

  ngOnDestroy() {
    this.bindQueryParamsManager.destroy();
  }
}

With this setup, the manager will take care of two things:

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

QueryParam Definition

queryKey

The query parameter key

path

The 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:

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

type

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

parser

Provide 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:

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

serializer

Provide 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:

const def = { serializer: (value) => (value instanceof Date ? value.toISOString().slice(0, 10) : (value as string)) };

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 the modelToUrl strategy, that will not update the control value when the page loads. When the data is available we can call the manager.syncDefs() method that'll update the controls based on the current query params:

@Component()
export class MyComponent {
  filters = new FormGroup({
    searchTerm: new FormControl(),
    users: new FormControl([]),
    someBoolean: new FormControl(false),
  });

  bindQueryParamsManager = this.factory
    .create<Filters>([
      { queryKey: 'searchTerm' },
      { queryKey: 'someBoolean', type: 'boolean' },
      { queryKey: 'users', type: 'array', strategy: 'modelToUrl' },
    ])
    .connect(this.filters);

  constructor(private factory: BindQueryParamsFactory) {}

  ngOnInit() {
    service.getUsers().subscribe((users) => {
      // Initalize the dropdown
      this.users = users;
      this.manager.syncDefs('users');
    });
  }

  ngOnDestroy() {
    this.bindQueryParamsManager.destroy();
  }
}

Note that syncDefs will always be called once under the hood.

Browser Support

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

Contributors

Thanks goes to these wonderful people (emoji key):


Netanel Basal

�� �� �� �� ��

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

Icons made by Freepik from www.flaticon.com
  • angular2 路由query参数,也就是?后面的参数,用来实现界面跳转,返回到原来界面保持查询条件,分页等效果 1、路由界面跳转,传递查询参数 queryParams对象 <a [routerLink]="['../bind-user']" [queryParams]='{qq: row.bind_qq_count, name: row.customer_name, state: row.s

  • 1. URLQuery参数处理 在 Gin 框架中下列方法可以用处理 URLQuery 参数: // 返回指定名字参数的值,c.Params.ByName(key) 简写, // 如: "/user/:id",则返回 id := c.Param("id") id == "john" func (c *Context) Param(key string) string

  • 尝试为将跟踪每日视图的表插入初始行时,出现错误: Fatal error: Call to a member function bind_param() on a non-object in /…/functions.PHP on line 157 该行是以下组的最后一行: if($stats_found) { $sqlquery = "UPDATE vid_stats SET views = ?

  • 我一直在学习为我的sql查询使用准备和绑定的语句,我已经出来了这个到目前为止,它的工作正常,但它是不是动态的,当涉及到多个参数或没有任何参数需要时, public function get_result($sql,$parameter) { # create a prepared statement $stmt = $this->mysqli->prepare($sql); # bind para

  • 1.params 配置路由格式:/router/:id 传递方式:在path后面跟上对应的值 传递后形成的路径:/router/123,/router/abc const routes=[ //路由配置页 { path:'/user/:userId', component:home } // 在app.vue写 <router-link :to="'/user/'+userId"

  • 我正在编写一个类,该类以编程方式创建给定某些列名称的表。 我正在使用PDO准备好的语句,这似乎引起了一些问题。 基本步骤如下: // create a query string to be sent to $pdo->prepare $sql = 'CREATE TEMP TABLE :tmp_table_name ( :person_id bigint, :encntr_id bigint, :

  • 一,【params】和【query】传值: 传值页面: <template> <div> <el-card class="post-card" v-for="item in postList" v-bind:key="item.id" v-on:click="turnToPost(item.id)"> </el-card> </div> </template>

  • sqlsrv_querysqlsrv_query 04/11/2019 本文内容 准备并执行语句。Prepares and executes a statement. 语法Syntax sqlsrv_query(resource $conn, string $tsql [, array $params [, array $options]]) 参数Parameters $conn:与已准备的语句相

 相关资料
  • 描述 (Description) 此函数将网络ADDRESS绑定到SOCKET标识的文件句柄。 ADDRESS应该是打开的套接字的适当类型的打包地址。 语法 (Syntax) 以下是此函数的简单语法 - bind SOCKET, ADDRESS 返回值 (Return Value) 此函数在失败时返回0,在成功时返回1。<!-- 例子 (Example) Following is the ex

  • 头文件: "boost/bind.hpp" Bind 库创建函数对象来绑定到一个函数(普通函数或成员函数)。不需要直接给出函数的所有参数,参数可以稍后给,这意味着绑定器可以用于创建一个改变了它所绑定到的函数的 arity (参数数量) 的函数对象,或者按照你喜欢的顺序重排参数。 函数 bind 的重载版本的返回类型是未指定的,即不能保证返回的函数对象的特征是怎样的。有时,你需要将对象存于某处,而不

  • bind(对socket定位) 相关函数 socket,accept,connect,listen 表头文件 #include<sys/types.h> #include<sys/socket.h> 定义函数 int bind(int sockfd,struct sockaddr * my_addr,int addrlen); 函数说明 bind()用来设置给参数sockfd的socket一个名称

  • bind 对socket定位 相关函数 socket,accept,connect,listen 表头文件 #include<sys/types.h> #include<sys/socket.h> 定义函数 int bind(int sockfd, struct sockaddr *my_addr, int addrlen); 函数说明 bind()用来设置给参数sockfd的socket一个

  • Bind是一款开放源码的DNS服务器软件,Bind由美国加州大学Berkeley分校开发和维护的,全名为Berkeley Internet Name Domain它是目前世界上使用最为广泛的DNS服务器软件,支持各种unix平台和windows平台。

  • This methode allows you to bind an object and sveral arguments to a function.