As you are implementing AngularJS applications, you will find that there are common JavaScript tasks that you need to perform regularly, such as comparing objects, deep copying, iterating through objects, and converting JSON data. AngularJS provides a lot of this basic functionality in the global APIs.
The global APIs are available when the angular.js
library is loaded, and you can access them by using the angular
object. For example, to create a deep copy of an object named myObj
, you use the following syntax:
var myCopy = angular.copy(myObj);
The following code shows an example of iterating through an array of objects by using the forEach()
global API:
var objArr = [{score: 95}, {score: 98}, {score: 92}];
var scores = [];
angular.forEach(objArr, function(value, key){
this.push(key + '=' + value);
}, scores);
// scores == ['score=95', 'score=98', 'score=92']
Table 2.1 lists some of the most useful utilities provided in the global APIs. You will see these used in a number of examples in this book.