Globals

This next group of functions can be executed from anywhere in Angular without having to inject them. They are mainly utility functions that allow you to do things easier or do things the Angular way.

Extend

This provides a way to combine two objects:

angular.extend(srcObject, destObject)

Parameters

  • srcObject(object): The object that extends will copy the properties from
  • Destobject(object): The object that extends will copy the properties to

Return value

This returns a reference of destObject.

Description

In JavaScript, there is no in-built way to extend an object using another object. This function does just that.

Here is a simple example that will extend one object with the other's property:

var first = { first: '1' };
var second = { second: '2' };
var extended = angular.extend(first, second);
//extended will be {first:'1', second:'2'}

noop

This is the no operation function:

angular.noop()

Parameters

  • The any or none functions are used as these functions does nothing, you can pass in no parameters or as many as you want.

Return value

This will return undefined.

Description

It is useful to have a function do no operation (noop). A great example of this is when you have a function as a parameter that is optional. If it is not passed in, you can run noop instead.

This is a simple example that demonstrates the scenario explained earlier:

function test(doSomething) {
  var callback = cb || angular.noop;
  callback('output');
}

isUndefined

This checks to see whether something is undefined:

angular.isUndefined(object)

Parameters

  • The object(any type) function can be any variable.

Return value

This returns a Boolean value.

Description

This states whether or not the variable is defined.

Copy

This makes a copy of an object:

angular.copy(srcObject, [destObject])

Parameters

  • srcObject(any type): This is the source object to be copied.
  • destObject(same type as srcObject): This is optional. If it is supplied, it would be the destination of the copy operation.

Return value

This function returns the copy of srcObject. If destObject is supplied, it would be returned.

Description

When you need to make a copy of an object instead of modifying the original object, use this function.

Bind

This binds a function to an object:

angular.bind(self, function, [args..])

Parameters

  • self(object): This will set this (the inner self-reference) in the function
  • Function(function): This is the function that is being bound
  • Args(any type): These are the arguments that will be bound to the function

Return value

This is the new bound function.

Description

This creates a new bound function that will execute in the context of self. This allows you to define a function and then execute it many times in different contexts. This is further extended by binding arguments as well.

This is a contrived example, but demonstrates the principles. First is a function that depends on the context to execute. It will add the first parameter to this and multiply that result:

function addAndMultiply(toAdd, toMultiply) {
  return (this + toAdd) * toMultiply;
}

Next, you will use angular.bind to create a new function:

var newFunc = angular.bind(4, addAndMultiply, 1);

This can be executed, and the return value will be 5 times the multiplier. In newFunc, this is 4, and toAdd is 1, so the inner parens will always be 5. For example this will return 10:

newFunc(2);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.231.15