AngularJS API

Ranjul Arumadi
Last Updated: May 13, 2022

Introduction

AngularJS is a framework used for building dynamic web apps. AngularJS lets us extend HTML vocabulary for our application. The resulting environment we get is expressive, readable, and quick to develop. API or Application Programming Interface is a software intermediary that allows applications to talk to each other. AngularJS APIs are used for comparing, iterating and converting objects. We will look at some of the AngularJS API in this blog.

Some AngularJS API

Let’s discuss some of the AngularJS APIs.

angular.isString()

It is used to check if an object is a string or not. True is returned if it is an object. Otherwise, false is returned.

Example:

<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="abc" ng-controller="contro">
<p>{{ object }}</p>
<p>{{ apiRes }}</p>
</div>
<script>
var app = angular.module('abc', []);
app.controller('contro', function($scope) {
$scope.object = "CodingNinjas";
           // Using the angular.isString() to check if the input is a string
$scope.apiRes = angular.isString($scope.object);
});
</script>
</body>
</html>

Output:

angular.lowercase()

angular.lowercase() is used to convert all the characters of the given string into lowercase characters.

Example:

<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="abc" ng-controller="contro">
<p>{{ object }}</p>
<p>{{ apiRes }}</p>
</div>
<script>
var app = angular.module('abc', []);
app.controller('contro', function($scope) {
 $scope.object = "CodingNinjas";
           // Using the angular.lowercase() to check if the input is in lowercase
 $scope.apiRes = angular.lowercase($scope.object);
});
</script>
</body>
</html>

Output:


angular.uppercase()

angular.uppercase() s used to convert all the characters of the given string into uppercase characters.

Example:

<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="abc" ng-controller="contro">
<p>{{ object }}</p>
<p>{{ apiRes }}</p>
</div>
<script>
var app = angular.module('abc', []);
app.controller('contro', function($scope) {
$scope.object = "CodingNinjas";
$scope.apiRes = angular.uppercase($scope.object);
});
</script>
</body>
</html>

Output:

angular.isNumber()

angular.isNumber() is used to check whether an object is a number or not. True is returned if the object is a number. Otherwise false is returned.

Example:

<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="abc" ng-controller="contro">
<p>{{ object }}</p>
<p>{{ apiRes }}</p>
</div>
<script>
var app = angular.module('abc', []);
app.controller('contro', function($scope) {
$scope.object = "CodingNinjas";
$scope.apiRes = angular.isNumber($scope.object);
});
</script>
</body>
</html>

Output:

angular.Date()

The angular.Date() function is used to check if the input is date or not. It returns true if the input is a date.

Example:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="MyApp" ng-controller="myController">
<p>First Text: {{ a }}</p>
<p>Result: {{ b }}</p>
<p>Second Text: {{ a1 }}</p>
<p>Result: {{ b1 }}</p>
</div>

<script>
var t = angular.module('MyApp', []);
t.controller('myController', function($scope) {
   $scope.a = "Ninjas";
   $scope.b = angular.isDate($scope.a);
   $scope.a1 = new Date();
   $scope.b1 = angular.isDate($scope.a1);
});
</script>

</body>
</html>

Output:

angular.copy()

angular.copy() creates a deep copy of an array of objects. Deep copy is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original.

Example:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="MyApp" ng-controller="myController">
<p>Text: {{ a }}</p>
<p>Copied Text: {{ b }}</p>
</div>

<script>
var t = angular.module('MyApp', []);
t.controller('myController', function($scope) {
   $scope.a = "Ninja";
   $scope.b = angular.copy($scope.a);
});
   </script>

   </body>
</html>

Output:

 

angular.toJson()

angular.toJson() converts javascript objects into JSON string format.

Example:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="MyApp" ng-controller="Controller">
<p>Input text: {{ input }}</p>
<p>Result: {{ output }}</p>
</div>

<script>
var t = angular.module('MyApp', []);
t.controller('Controller', function($scope) {
    $scope.input = "JSON";
    $scope.output = angular.toJson($scope.input);

});
</script>

</body>
</html>

Output:

angular.isString()

angular.isString() checks whether the reference is string or not. If the reference is string it is true, else it returns false.

Example:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="MyApp" ng-controller="myController">
<p>Text 1: {{ a }}</p>
<p>{{ a1 }}</p>
<p>Text 2: {{ b }}</p>
<p>{{ b1 }}</p>
</div>

<script>
var x = angular.module('MyApp', []);
x.controller('myController', function($scope) {
    $scope.a = "Coder";
    $scope.a1 = angular.isString($scope.a);
    $scope.b = 987;
    $scope.b1 = angular.isString($scope.b);

});
</script>

</body>
</html>

Output:

Frequently Asked Questions

 

1. How can I convert a given string to lowercase?

Ans. You can use angular.lowercase() to convert a given string to lowercase.

 

2. What are some tasks that the AngularJS API can do?

Ans. The AngularJS API can perform tasks like comparison of objects, iterating objects and converting data.

 

Key Takeaways

AngularJS API is used for comparing, iterating and converting objects. In this blog, we have discussed AngularJS API.

 

If you loved reading this article about the AngularJS API, check out AngularJS vs Angular: Differences Between The Two and JavaScript Cheat Sheet: For a New Developer.

Was this article helpful ?
1 upvote