AngularJS API

API stands for application programming interface. It is a set of built-in functions that are used to perform common tasks. AngularJS API also consists of built-in utility functions that different tasks such as object comparison, case conversion, object iteration etc. In this article we shall some of the most commonly used AngularJS API functions. All the AngularJS API functions are called via “angular” object. Take a look at the following example to see how we can change the case of text via API functions.

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

<div ng-app="NewApp" ng-controller="NewCtrl">

<p>{{ name1 }}</p>
<p>{{ name2 }}</p>
<p>{{ gender1 }}</p>
<p>{{ gender2 }}</p>
</div>

<script>
var app = angular.module('NewApp', []);
app.controller('NewCtrl', function($scope) {
    $scope.name1 = "mike";
    $scope.name2 = angular.uppercase($scope.name1);
    
    $scope.gender1 = "MALE";
       $scope.gender2 = angular.lowercase($scope.gender1);
});
</script>

</body>
</html>

Download the code Try the code

Take a look at the above code. Go to the script section at the bottom of the page. Here we initialize a controller and create four variables via $scope object. First variable “name1” contains some random name in lowercase. We use angular.uppercase API function to convert the value in the “name1” variable to uppercase. We then store the resultant uppercase value to another variable “name2”. Similary, “gender1” contains value “MALE” in uppercase. We use angular.lowercase API function to convert the value in the “gender1” variable to lowercase. We then store the resultant lowercase value to another variable “gender2”. Finally we print all the four variables on screen to make sure that API functions are performing correctly.

IsNumber & IsString Functions

These functions validate if a value is string or number respectively. Take a look at the following example.

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

<div ng-app="NewApp" ng-controller="NewCtrl">

<p>{{ message1 }}</p>
<p>{{ message2 }}</p>
<p>{{ number1 }}</p>
<p>{{ number2 }}</p>
</div>

<script>
var app = angular.module('NewApp', []);
app.controller('NewCtrl', function($scope) {
    $scope.message1 = "Welcome to knowledge hills";
    $scope.message2 = angular.isString($scope.message1);
    
    $scope.number1 = 200;
    $scope.number2 = angular.isNumber($scope.number1);
});
</script>

</body>
</html>

Download the code Try the code

Take a look at the above code. Go to the script section at the bottom of the page. Here we initialize a controller and create four variables via $scope object. First variable “message1” contains some random string. We use angular.isString() API function to check whether “message1” contains a valid string or not. We then store the resultant boolean value to another variable “message2”. Similary, “number1” contains some random numeric value. We use angular.isNumber() API function to check whether “number1” contains a valid string or not. We then store the resultant boolean value to another variable “number2”. Finally we print all the four variables on screen to make sure that API functions are performing correctly.

<<< AngularJS ValidationAngularJS Modules >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .