In this tutorial, you’ll learn about essential AngularJS APIs, their usage, and applications with the help of working examples.
While implementing AngularJS applications, we use various JavaScript functions frequently, at different places in our application, such as comparing two objects, deep copying, iterating through objects, and converting JSON data and so on. AngularJS implements this basic functionality in its global APIs.
These global API’s, become available for use, after the loading of the <angular.js>
library. To access these API’s, we have to use an angular object.
Let’s now discuss the most frequently used global API’s supported by AngularJS.
<Copy>
FunctionAngularJS <copy>
function intends to create, a deep copy of source object or array and assign it to a destination, where the destination is optional.
angular.copy(source, [destination]);
<Copy>
Function Example<!DOCTYPE html>
<html ng-app="">
<head> <!-- www.techbeamers.com -->
<title>AngularJS sample code</title>
<script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</script>
</head>
<body bgcolor="#bnde45">
<fieldset>
<legend>AngularJS Copy Function</legend>
<script>
var src = {'subject' : 'english', 'score' : '90'};
var dest = {};
angular.copy(src, dest);
document.write("Subject : " + dest.subject + " Marks : " + dest.score);
</script>
</fieldset>
</body>
</html>
<Extend>
FunctionSome scenarios require creating a copy of one or more source objects into the destination object (also called as the shallow copy).
AngularJS <extend>
function allows us to do the same. Thus, it accepts destination object and one or more source objects (that we want to copy into destination object) as its parameters.
var obj = angular.extend({}, obj1, obj2,...);
<Extend>
Function Example
var person = { 'Name': 'Maya', 'Age': '25', 'Skills': { 'name': dancing', place': 'Delhi' } };
var job = { 'Title': 'Programmer', 'Experience': '5', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } };
console.log(angular.extend({}, person, job));
//output : {{ 'Name': 'Maya', 'Age': '25', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } , 'Title': 'Programmer', 'Experience': '5'}
<Equals>
FunctionAngularJS <equals>
function is used to verify that two objects or two values are equivalent or not. It supports value types, regular expressions, arrays, and objects.
angular.equals(obj1, obj2);
<Equals>
Function Examplevar myObj1 = {'subject' : 'English', 'score' : '90'};
var myObj2 = {'subject' : 'English', 'score' : '90'};
var myObj3 = {'class' : 'fifth'};
document.write(angular.equals(myObj1, myObj2));
document.write("</br>");
document.write(angular.equals(myObj1, myObj3));
document.write("</br>");
<ForEach>
FunctionThe <forEach>
function allows iterating through each object in the obj collection, which can be an object or an array. The element can be determined using the key of object or index of an array.
angular.forEach(obj, iterator, [context]);
Where obj is an object or an array. And the iterator specifies a function to call, using the following
function(value, key)
Here, the value is the element of an object or an array. And, Key is the array index or the object key.
The <context>
parameter specifies a JavaScript object that acts as the reference. It can be accessed using the <this>
keyword, inside the <forEach>
loop.
<ForEach>
Function Examplevar values = [{'subject' : 'English', 'score' : '90'},{'subject' : 'Maths', 'score' : '99'},{'subject' : 'Computers', 'score' : '95'}];
angular.forEach(values, function(value, key) {
document.write(value.subject);
document.write(" ");
});
<FromJson>
FunctionAngularJS <fromJson>
function intends to serialize input value into a JSON formatted string.
angular.fromJson(json);
<FromJson>
Function Examplevar strJSON= '{"subject" : "English", "score" : '90', "class" : "fifth"}';
document.write(angular.fromJson(strJSON));
<ToJson>
FunctionAngularJS <toJson>
function intends to serialize input into a JSON formatted string.
angular.toJson(obj, pretty);
<ToJson>
Function Examplevar sampleStr = {'subject' : 'English', 'score' : '90', 'class' : 'fifth'}
document.write(angular.toJson(sampleStr));
<IsUndefined>
FunctionAngularJS <isUndefined>
function returns true, if the value parameter passed in it is not defined.
angular.isUndefined(value);
<IsUndefined>
Function Example.var sampleObj;
var str = "AngularJS Tutorial";
document.write(angular.isUndefined(sampleObj));
document.write(" ");
document.write(angular.isUndefined(str));
<IsDefined>
FunctionAngularJS <isDefined>
function returns true, if the value parameter passed in it is defined as an object.
angular.isDefined(value);
<IsDefined>
Function Example.var sampleObj;
var str = "AngularJS Tutorial";
document.write(angular.isDefined(sampleObj));
document.write(" ");
document.write(angular.isDefined(str));
<IsObject>
FunctionAngularJS <isObject>
function returns true, if the value parameter passed in it is a JavaScript object.
angular.isObject(value);
<IsNumber>
FunctionAngularJS <isNumber>
function returns true, if the value parameter passed in it is a number.
angular.isNumber(value);
<IsDate>
FunctionAngularJS <isDate>
function returns true, if the value parameter passed in it is a Date object.
angular.isDate(value);
<IsString>
FunctionAngularJS <isString>
function returns true, if the value parameter passed in it is a String object.
angular.isString(value);
<IsArray>
FunctionAngularJS <isArray>
function returns true, if the value parameter passed in it is an Array object.
angular.isArray(value);
<IsArray>
Function Examplevar myArray = [20,40,60,80,100,120];
var str = "AngularJS Tutorial";
document.write(angular.isArray(myArray));
document.write("</br>");
document.write(angular.isArray(str));
<IsFunction>
FunctionAngularJS <isFunction>
function returns true, if the value parameter passed in it is a JavaScript function.
angular.isFunction(value);
<IsFunction>
Examplefunction messageStr()
{
return "AngularJS Tutorial";
}
document.write(angular.isFunction(messageStr));
<IsElement>
FunctionAngularJS <isElement>
function returns true, if the value parameter passed in, is either a DOM element object or a jQuery element object.
angular.isElement(value);
<IsElement>
Function Example<!DOCTYPE html>
<html ng-app="">
<head> <!-- www.techbeamers.com -->
<title>AngularJS Sample code</title>
<script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</script>
</head>
<body bgcolor="#bnde45">
<fieldset>
<legend>AngularJS isElement Function</legend>
<script>
document.write(angular.isElement('title'));
document.write("</br>");
</script>
</fieldset>
</body>
</html>
<Uppercase>
FunctionThis function converts the input text or string parameter into its upper case version.
angular.uppercase(str);
<Lowercase>
FunctionThis function converts the input text or string parameter into its lower case version.
angular.lowercase(str);
<Merge>
FunctionThis function creates a deep copy of one or more source objects into the destination object. In a way, it is similar to AngularJS <extend>
function. However, unlike the <extend>
function, <merge>
descends recursively into object properties of source objects, thus creating a deep copy. Functional parameters include one destination and one or more source objects.
var object = angular.merge({}, obj1, obj2);
<Merge>
Examplevar person = { 'Name': 'Maya', 'Age': '25', 'Skills': { 'name': dancing', place': 'Delhi' } };
var job = { 'Title': 'Programmer', 'Experience': '5', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true' } };
console.log(angular.merge({}, person, job));
//output : { 'Name': 'Maya', 'Age': '25', 'Skills': { 'name': 'Designing', 'experience': '2', 'certified': 'true', 'place': 'Delhi' }, 'Title': 'Programmer', 'Experience': '5' };
<Module>
FunctionThis function is used to create, register and retrieve Angular modules. It acts as a container for different parts of AngularJS application like controllers, services, filters, directives, etc. AngularJS allows you to declare a module using the angular.module() method.
angular.module(name, [requires], [configFn]);
<Module>
Function Example
<html>
<head>
<title>AngularJS sample code for modules function</title>
<Script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">
</Script>
<Script>
//Defining Controller Using AngularJS Module
var app = angular.module('myApp', []);
app.controller('sampleController', function($scope) {
$scope.student = { subject: "English", class: "fifth", score: 90 };
});
</Script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="sampleController">
<p>Student Subject : {{ student.subject }}</p>
<p>Student Class : {{ student.class }}</p>
<p>Student Score : {{ student.score }}</p>
</div>
</div>
</body>
</html>
//Output
Student Subject : English
Student Class : fifth
Student Score : 90
<Bind>
FunctionThis function is used to bind the current context to a function, but actually, execute it at a later time. It allows working with partial functions.
angular.bind(self, fn, [args]);
<Bind>
Function Example<!DOCTYPE html>
<html >
<head>
<title>AngularJS Sample code</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="bindController">
angular.bind value = {{Add}}
</div>
</body>
</html>
<script>
var app = angular.module("myApp", []);
app.controller('bindController', ['$scope', function ($scope) {
function addition(x, y) {
return x + y;
}
var data = angular.bind(this, addition, 10);
$scope.Add = data(5);
}]);
</script>
//Output
angular.bind value = 15
We hope the above tutorial would have helped you cover the most commonly used AngularJS APIs with ease. We’ve attached supporting examples in almost all the APIs so that it is easy for you to understand the API and its usage.
In case, you’d any query regarding the API explanation given above, then do let us know.
Thanks for reading !
#angular #angularrjs