Services are among the strongest features of AngularJS. It is mainly concerned with the reusability of the code. The Angular Services object has properties and method objects just like any other JavaScript object. The Angular JS Services make the application more manageable and testable.
Angular provides several useful services like $http, $log, $filter etc. There are many more others built-in services provided by Angular. We can also create our own Angular Service.
Note
The built-in services provided by Angular are always prefixed with a dollar ($) sign.
Use of $timeout Service
Step 1
Create a blank project.
Step 2
Create a blank HTML Page.
Step 3
In the Head section of the HTML page, set the CDN link of AngularJS.
- <head>
- <script src="https:
- </head>
Step 4
Now, create a script section under head tag to write AngularJS script.
- <script>
- var app = angular.module('myApp', []);
- app.controller('myCtrl', function($scope, $timeout) {
- $scope.myHeader = "Hello friends!";
- $timeout(function() {
- $scope.message = "Hope you enjoying Angular?";
- }, 4000);
- });
- </script>
In the above code, we simply passed the $timeout service to our controller and set the time out duration (in milliseconds).
Step 5
Create a div section within the body and register your ng-app and ng-controller.
- <div ng-app="myApp" ng-controller="myCtrl">
- <p>This header will change after four seconds </p>
- <h1>{{message}}</h1>
- </div>
How to create your own service
In this program, we will add a space after every Upper case letter in the output
HTML Page
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head>
- <title></title>
- <script src="scripts/angular.js"></script>
- <script src="scripts/MyScript.js"></script>
- <script src="scripts/StringTransformationService.js"></script>
- <link href="Styles.css" rel="stylesheet" /> </head>
-
- <body ng-app="myModule">
- <div ng-controller="myController">
- <table>
- <tr>
- <td>Your String</td>
- <td><input type="text" ng-model="input" /> </td>
- </tr>
- <tr>
- <td>Result</td>
- <td><input type="text" ng-model="output" /></td>
- </tr>
- <tr>
- <td></td>
- <td> <input type="button" ng-click="ChangeString(input)" value="Prcess" /> </td>
- </tr>
- </table>
- </div>
- </body>
-
- </html>
MyScript.js Page
In the above code, we’ve created our controller and passed our own custom service named “MyStringTransformationService”. We’ve also created a function that will call the service and that function will receive one argument as “input”].
Now, we will create our own custom service.
- Add a JS file to the project and name it as stringReverseService.js.
- Now, copy and paste the below code. In this example, we are using the factory method to create and register our service.
StringTransformationService.js
- app.factory("MyStringTransformationService", function() {
- return {
- ChangeString: function(input) {
- if (!input) {
- return input;
- }
- var output = "";
- for (var i = 0; i < input.length; i++) {
- if (i > 0 && input[i] == input[i].toUpperCase()) {
- output = output + " ";
- }
- output = output + input[i];
- }
- return output;
- }
- }
- });
For more information, please visit my
Blog.