ParamType | UI-Router
Options
All
  • Public
  • Public/Protected
  • All
Menu

Class ParamType

A class that implements Custom Parameter Type functionality.

This class has naive implementations for all the ParamTypeDefinition methods.

An instance of this class is created when a custom ParamTypeDefinition object is registered with the UrlMatcherFactory.type.

Used by UrlMatcher when matching or formatting URLs, or comparing and validating parameter values.

example

{
  decode: function(val) { return parseInt(val, 10); },
  encode: function(val) { return val && val.toString(); },
  equals: function(a, b) { return this.is(a) && a === b; },
  is: function(val) { return angular.isNumber(val) && isFinite(val) && val % 1 === 0; },
  pattern: /\d+/
}

Hierarchy

  • ParamType

Implements

Index

Constructors

  • Parameters

    • def: ParamTypeDefinition
      :

      A configuration object which contains the custom type definition. The object's properties will override the default methods and/or pattern in ParamType's public interface.

    Returns ParamType

    :

    a new ParamType object


Properties

dynamic: boolean
name: string
pattern: RegExp
raw: boolean

Methods

  • $asArray(mode: any, isSearch: any): any
  • Wraps an existing custom ParamType as an array of ParamType, depending on 'mode'. e.g.:

    • urlmatcher pattern "/path?{queryParam[]:int}"
    • url: "/path?queryParam=1&queryParam=2
    • $stateParams.queryParam will be [1, 2] if mode is "auto", then
    • url: "/path?queryParam=1 will create $stateParams.queryParam: 1
    • url: "/path?queryParam=1&queryParam=2 will create $stateParams.queryParam: [1, 2]
  • Wraps an existing custom ParamType as an array of ParamType, depending on 'mode'. e.g.:

    • urlmatcher pattern "/path?{queryParam[]:int}"
    • url: "/path?queryParam=1&queryParam=2
    • $stateParams.queryParam will be [1, 2] if mode is "auto", then
    • url: "/path?queryParam=1 will create $stateParams.queryParam: 1
    • url: "/path?queryParam=1&queryParam=2 will create $stateParams.queryParam: [1, 2]

    Parameters

    • mode: any
    • isSearch: any

    Returns any


  • $normalize(val: any): any
  • Given an encoded string, or a decoded object, returns a decoded object

  • Given an encoded string, or a decoded object, returns a decoded object

    Parameters

    • val: any

    Returns any


  • $subPattern(): string
  • decode(val: string, key?: string): any
  • Decodes a parameter value string (from URL string or transition param) to a custom/native value.

  • Decodes a parameter value string (from URL string or transition param) to a custom/native value.

    For example, if your type decodes to an array of ints, then decode the string as an array of ints here: (str) => str.split("-").map(str => parseInt(str, 10))

    Note: in general, encode and decode should be symmetrical. That is, encode(decode(str)) === str

    Parameters

    • val: string
    • key Optional: string

    Returns any

    :

    a custom representation of the URL parameter value.


  • encode(val: any, key?: string): string | Array<string>
  • Encodes a custom/native type value to a string that can be embedded in a URL.

  • Encodes a custom/native type value to a string that can be embedded in a URL.

    Note that the return value does not need to be URL-safe (i.e. passed through encodeURIComponent()), it only needs to be a representation of val that has been encoded as a string.

    For example, if your type decodes to an array of ints, then encode the array of ints as a string here: (intarray) => intarray.join("-")

    Note: in general, encode and decode should be symmetrical. That is, encode(decode(str)) === str

    Parameters

    • val: any
    • key Optional: string

    Returns string | Array<string>

    :

    a string representation of val that can be encoded in a URL.


  • equals(a: any, b: any): boolean
  • Determines whether two decoded values are equivalent.

  • Determines whether two decoded values are equivalent.

    For example, if your type decodes to an array of ints, then check if the arrays are equal: (a, b) => a.length === b.length && a.reduce((acc, x, idx) => acc && x === b[idx], true)

    Parameters

    • a: any
    • b: any

    Returns boolean

    :

    true if the values are equivalent/equal, otherwise false.


  • is(val: any, key?: string): boolean
  • Tests if some object type is compatible with this parameter type

  • Tests if some object type is compatible with this parameter type

    Detects whether some value is of this particular type. Accepts a decoded value and determines whether it matches this ParamType object.

    If your custom type encodes the parameter to a specific type, check for that type here. For example, if your custom type decodes the URL parameter value as an array of ints, return true if the input is an array of ints: (val) => Array.isArray(val) && array.reduce((acc, x) => acc && parseInt(val, 10) === val, true). If your type decodes the URL parameter value to a custom string, check that the string matches the pattern (don't use an arrow fn if you need this): function (val) { return !!this.pattern.exec(val) }

    Note: This method is not used to check if the URL matches. It's used to check if a decoded value is this type. Use pattern to check the URL.

    Parameters

    • val: any
    • key Optional: string

    Returns boolean

    :

    true if the value matches the type, otherwise false.


  • toString(): string

Generated using TypeDoc