We are no longer accepting contributions to Documentation. Please see our post on meta.

AngularJS

Built-in helper Functions All Versions

0.9.0
0.9.1
0.9.2
0.9.3
0.9.4
0.9.5
0.9.6
0.9.7
0.9.9
0.9.10
0.9.11
0.9.12
0.9.13
0.9.14
0.9.15
0.9.16
0.9.17
0.9.18
0.9.19
0.10.0
0.10.1
0.10.2
0.10.3
0.10.4
0.10.5
0.10.6
1.0.0rc1
g3-v1.0.0rc1
g3-v1.0.0-rc2
v1.0.0rc2
v1.0.0rc3
v1.0.0rc4
v1.0.0rc5
v1.0.0rc6
v1.0.0rc7
v1.0.0rc8
v1.0.0rc9
v1.0.0rc10
v1.0.0rc11
v1.0.0rc12
1.0.0
1.0.1
1.0.2
1.1.0
1.0.3
1.1.1
1.0.4
1.1.2
1.0.5
1.1.3
1.0.6
1.1.4
1.0.7
1.1.5
1.2.0rc1
1.0.8
1.2.0-rc.2
1.2.0-rc.3
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0-beta.1
1.3.0-beta.2
1.3.0-beta.3
1.2.15
1.3.0-beta.4
1.2.16
1.3.0-beta.5
1.3.0-beta.6
1.3.0-beta.7
1.3.0-beta.8
1.3.0-beta.9
1.3.0-beta.10
1.2.17
1.3.0-beta.11
1.2.18
1.3.0-beta.12
1.3.0-beta.13
1.2.19
1.3.0-beta.14
1.2.20
1.3.0-beta.15
1.3.0-beta.16
1.2.21
1.3.0-beta.17
1.2.22
1.3.0-beta.18
1.2.23
1.3.0-beta.19
1.3.0-rc.0
1.2.24
1.3.0-rc.1
1.2.25
1.3.0-rc.2
1.3.0-rc.3
1.3.0-rc.4
1.2.26
1.3.0-rc.5
1.3.0
1.3.1
1.3.2
1.3.3
1.2.27
1.3.4
1.3.5
1.3.6
1.3.7
1.2.28
1.3.8
1.4.0-beta.0
1.3.9
1.3.10
1.4.0-beta.1
1.3.11
1.4.0-beta.2
1.3.12
1.4.0-beta.3
1.3.13
1.4.0-beta.4
1.3.14
1.4.0-beta.5
1.3.15
1.4.0-beta.6
1.4.0-rc.0
1.4.0-rc.1
1.4.0-rc.2
1.4.0
1.3.16
1.4.1
1.3.17
1.4.2
1.4.3
1.4.4
1.3.18
1.4.5
1.3.19
1.4.6
1.5.0-beta.0
1.2.29
1.3.20
1.4.7
1.5.0-beta.1
1.5.0-beta.2
1.4.8
1.5.0-rc.0
1.5.0-rc.1
1.4.9
1.5.0-rc.2
1.5.0
1.4.10
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.4.11
1.5.6
1.4.12
1.5.7
1.2.30
1.5.8
1.2.31
1.4.13
1.2.32
1.6.0-rc.0
1.6.0-rc.1
1.5.9
1.6.0-rc.2
1.6.0
1.5.10
1.6.1
1.5.11
1.6.2
1.6.3
1.6.4
1.6.5

This draft deletes the entire topic.

Examples

  • 5

    The angular.equals function compares and determines if 2 objects or values are equal, angular.equals performs a deep comparison and returns true if and only if at least 1 of the following conditions is met.

    angular.equals(value1, value2)

    1. If the objects or values pass the === comparison
    2. If both objects or values are of the same type, and all of their properties are also equal by using angular.equals
    3. Both values are equal to NaN
    4. Both values represent the same regular expression's result.

    This function is helpful when you need to deep compare objects or arrays by their values or results rather than just references.

    Examples

    angular.equals(1, 1) // true
    angular.equals(1, 2) // false
    angular.equals({}, {}) // true, note that {}==={} is false
    angular.equals({a: 1}, {a: 1}) // true
    angular.equals({a: 1}, {a: 2}) // false
    angular.equals(NaN, NaN) // true
    
  • 4

    The function angular.toJson will take an object and serialize it into a JSON formatted string.

    Unlike the native function JSON.stringify, This function will remove all properties beginning with $$ (as angular usually prefixes internal properties with $$)

    angular.toJson(object)
    

    As data needs to be serialized before passing through a network, this function is useful to turn any data you wish to transmit into JSON.

    This function is also useful for debugging as it works similarly to a .toString method would act.

    Examples:

    angular.toJson({name: "barf", occupation: "mog", $$somebizzareproperty: 42})
    // "{"name":"barf","occupation":"mog"}"
    angular.toJson(42)
    // "42"
    angular.toJson([1, "2", 3, "4"])
    // "[1,"2",3,"4"]"
    var fn = function(value) {return value}
    angular.toJson(fn)
    // undefined, functions have no representation in JSON
    
  • 3

    The angular.copy function takes an object, array or a value and creates a deep copy of it.

    angular.copy()

    Example:

    Objects:

    let obj = {name: "vespa", occupation: "princess"};
    let cpy = angular.copy(obj);
    cpy.name = "yogurt"
    // obj = {name: "vespa", occupation: "princess"}
    // cpy = {name: "yogurt", occupation: "princess"}
    

    Arrays:

    var w = [a, [b, [c, [d]]]];
    var q = angular.copy(w);
    // q = [a, [b, [c, [d]]]]
    

    At the above example angular.equals(w, q) will evaluate to true because .equals tests equality by value. however w === q will evaluate to false because strict comparison between objects and arrays is done by reference.

  • 1

    The angular.isArray function returns true if and only if the object or value passed to it is of the type Array.

    angular.isArray(value)

    Examples

    angular.isArray([]) // true
    angular.isArray([2, 3]) // true
    angular.isArray({}) // false
    angular.isArray(17) // false
    

    It is the equivalent of

    Array.isArray(someValue)
    
  • 1

    The angular.isDate function returns true if and only if the object passed to it is of the type Date.

    angular.isDate(value)

    Examples

    angular.isDate("lone star") // false
    angular.isDate(new Date()) // true
    
  • 1

    The function angular.isDefined tests a value if it is defined

    angular.isDefined(someValue)

    This is the equivalent of performing

    value !== undefined; // will evaluate to true is value is defined
    

    Examples

    angular.isDefined(42) // true
    angular.isDefined([1, 2]) // true
    angular.isDefined(undefined) // false
    angular.isDefined(null) // true
    

    The function angular.isUndefined tests if a value is undefined (it is effectively the opposite of angular.isDefined)

    angular.isUndefined(someValue)

    This is the equivalent of performing

    value === undefined; // will evaluate to true is value is undefined
    

    Or just

    !angular.isDefined(value)
    

    Examples

    angular.isUndefined(42) // false
    angular.isUndefined(undefined) // true
    
  • 1

    The angular.isElement returns true if the argument passed to it is a DOM Element or a jQuery wrapped Element.

    angular.isElement(elem)

    This function is useful to type check if a passed argument is an element before being processed as such.

    Examples:

    angular.isElement(document.querySelector("body"))
    // true
    angular.isElement(document.querySelector("#some_id"))
    // false if "some_id" is not using as an id inside the selected DOM
    angular.isElement("<div></div>")
    // false
    
  • 1

    The function angular.isFunction determines and returns true if and only if the value passed to is a reference to a function.

    The function returns a reference to the now extended destination object

    angular.isFunction(fn)

    Examples

    var onClick = function(e) {return e};
    angular.isFunction(onClick); // true
    
    var someArray = ["pizza", "the", "hut"];
    angular.isFunction(someArray ); // false
    
  • 1

    The function angular.isString returns true if the object or value given to it is of the type string

    angular.isString(value1)

    Examples

    angular.isString("hello") // true
    angular.isString([1, 2]) // false
    angular.isString(42) // false
    

    This is the equivalent of performing

    typeof someValue === "string"
    
  • 1

    The function angular.merge takes all the enumerable properties from the source object to deeply extend the destination object.

    The function returns a reference to the now extended destination object

    angular.merge(destination, source)

    Examples

    angular.merge({}, {}) // {} 
    angular.merge({name: "king roland"}, {password: "12345"})
    // {name: "king roland", password: "12345"}
    angular.merge({a: 1}, [4, 5, 6]) // {0: 4, 1: 5, 2: 6, a: 1}
    angular.merge({a: 1}, {b: {c: {d: 2}}}) // {"a":1,"b":{"c":{"d":2}}}
    
  • 1

    The angular.noop is a function that performs no operations, you pass angular.noop when you need to provide a function argument that will do nothing.

    angular.noop()

    A common use for angular.noop can be to provide an empty callback to a function that will otherwise throw an error when something else than a function is passed to it.

    Example:

    $scope.onSomeChange = function(model, callback) {
        updateTheModel(model);
        if (angular.isFunction(callback)) {
            callback();
        } else {
            throw new Error("error: callback is not a function!");
        }
    };
    
    $scope.onSomeChange(42, function() {console.log("hello callback")});
    // will update the model and print 'hello callback'
    $scope.onSomeChange(42, angular.noop);
    // will update the model
    

    Additional examples:

    angular.noop() // undefined
    angular.isFunction(angular.noop) // true
    
  • 0

    The angular.forEach accepts an object and an iterator function. It then runs the iterator function over each enumerable property/value of the object. This function also works on arrays.

    Like the JS version of Array.prototype.forEach The function does not iterate over inherited properties (prototype properties), however the function will not attempt to process a null or an undefined value and will just return it.

    angular.forEach(object, function(value, key) { // function});

    Examples:

    angular.forEach({"a": 12, "b": 34}, (value, key) => console.log("key: " + key + ", value: " + value))
    // key: a, value: 12
    // key: b, value: 34
    angular.forEach([2, 4, 6, 8, 10], (value, key) => console.log(key))
    // will print the array indices: 1, 2, 3, 4, 5
    angular.forEach([2, 4, 6, 8, 10], (value, key) => console.log(value))
    // will print the array values: 2, 4, 6, 7, 10
    angular.forEach(undefined, (value, key) => console.log("key: " + key + ", value: " + value))
    // undefined
    
    
  • 0

    The function angular.fromJson will deserialize a valid JSON string and return an Object or an Array.

    angular.fromJson(string|object)

    Note that this function is not limited to only strings, it will output a representation of any object passed to it.

    Examples:

    angular.fromJson("{\"yogurt\": \"strawberries\"}")
    // Object {yogurt: "strawberries"}
    angular.fromJson('{jam: "raspberries"}')
    // will throw an exception as the string is not a valid JSON
    angular.fromJson(this)
    // Window {external: Object, chrome: Object, _gaq: Y, angular: Object, ng339: 3…}
    angular.fromJson([1, 2])
    // [1, 2]
    typeof angular.fromJson(new Date())
    // "object"
    
  • 0

    The angular.identity function returns the first argument passed to it.

    angular.identity(argument)

    This function is useful for functional programming, you can provide this function as a default in case an expected function was not passed.

    Examples:

    angular.identity(42) // 42
    
    var mutate = function(fn, num) {
        return angular.isFunction(fn) ? fn(num) : angular.identity(num)
    }
    
    mutate(function(value) {return value-7}, 42) // 35
    mutate(null, 42) // 42
    mutate("mount. rushmore", 42) // 42
    
  • 0

    The angular.isNumber function returns true if and only if the object or value passed to it is of the type Number, this includes +Infinity, -Infinity and NaN

    angular.isNumber(value)

    This function will not cause a type coercion such as

    "23" == 23 // true 
    

    Examples

    angular.isNumber("23") // false
    angular.isNumber(23) // true
    angular.isNumber(NaN) // true
    angular.isNumber(Infinity) // true
    

    This function will not cause a type coercion such as

    "23" == 23 // true 
    
  • 0

    The angular.isObject return true if and only if the argument passed to it is an object, this function will also return true for an Array and will return false for null even though typeof null is object .

    angular.isObject(value)

    This function is useful for type checking when you need a defined object to process.

    Examples:

    angular.isObject({name: "skroob", job: "president"})
    // true
    angular.isObject(null)
    // false
    angular.isObject([null])
    // true
    angular.isObject(new Date())
    // true
    angular.isObject(undefined)
    // false
    
Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about Built-in helper Functions? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.