Thursday, 19 April 2018

007 AngularJS Service

AngularJS Service

AngularJS support several kind of service .Service are Javascript functions that do some specific works .The service are unique function and can be test separately . Services are normally injected using dependency injection mechanism . AngularJS contain more than 30 inbuilt service and you can build your own service also.Service always starts with $ prefix .Below are the list of inbuilt service of AngularJS.


List of Service
$anchorScroll $animate $animateCss
$cacheFactory $templateCache $compile
$controller $document $exceptionHandler
$filter $httpParamSerializer $httpParamSerializerJQLike
$http $xhrFactory $httpBackend
$interpolate $interval $jsonpCallbacks
$locale $location $log
$parse $q $rootElement
$rootScope $sceDelegate $sce
$templateRequest $timeout $window

Out of 30 inbuilt service , we are discussing here some of the service application.

$http : $http send request to the remote server and handle the response data.The response from server is managed with several properties.
    .data : 
response object ,array,string from server.
    .status :
HTTP status a number. 
    .statusText :
HTTP status defining string.

<div style="background-color: #EAEAEA;">
<!DOCTYPE >
   <head>
                <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">   </script>
    </head>
    <script>
             var app = angular.module('myApp', []);
                    app.controller('myControl', function ($scope, $http) 
            {
                           $http.get("myHtmlPage.htm").then(function (response) 
                 {
                                $scope.data = response.data;
                             });
                  });
</script>
<div ng-app="myApp">
<div ng-controller="myControl">
{{data}}
</div>
</div>

myHtmlPage.htm
Thanks ,for comming here

Output :
Thanks ,for comming here


$interval : $interval is same like window.setInterval of Javascript.

The function execute every delay milliseconds.$interval can be cancelled by "cancel" method . $interval.cancel() stop interval counting.

Example

<div style="background-color: #EAEAEA;">
<!DOCTYPE >
<head>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<script>
         var app = angular.module('myApp', []);
               app.controller('myControl', function ($scope, $interval) 
         {
                         $scope.data = '';
                         $interval(function () {
                         $scope.data = $scope.data + '*';
              }, 1000);
});
</script>
<div ng-app="myApp">
<div ng-controller="myControl">
     {{data}}
</div>
</div>

Ouput :
*
Ouput :
 **
Ouput :
 ***

$log : Writes the message into the browser's console.This service is to simplify debugging and troubleshooting.This service has several method .
    log:
call when to write a log message
    info:
call when to write an information message
    warn:
call when to write a warning message
    error:
call when to write an error message
    debug:
call when to write a debug message
To see the output , press F12 and goto console,you will see the message

Example

<div style="background-color: #EAEAEA;">
<!DOCTYPE >
<head>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<script>
           var myApp = angular.module('myApp', []);

             myApp.controller("myControl", function ($log) 
        {
                       $log.log('This is log.');
                       $log.error('This is error.');
                       $log.info('This is info.');
                       $log.warn('This is warning.');
                      $log.debug('This is debugging.');
           });
</script>
<div ng-app="myApp">
           <div ng-controller="myControl">
             Press F12 ,Go to Console.
         </div>
</div>

Output :



$window : An object reference to the browser's window object. AngularJS we always refer to it through the $window service.$window can be override , easily test,alert .prompt can be generated.

Example


<!DOCTYPE >
<head>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<script>
             var app = angular.module("myApp", []);
              app.controller("myCtroller", function ($scope, $window)
          {
                 $scope.ShwoMessage = function ()
                   {
                     $scope.message = '';
                     $scope.message = $scope.DelIndex;
                     $window.alert($scope.message);
                  }
                  $scope.ShwoPrompt = function () 
           {
                     $scope.msg = '';
                     $scope.msg = $scope.DelIndex;
                     var name = $window.prompt($scope.msg);
                 }
});

</script>
<div ng-app="myApp" ng-controller="myCtroller" >
        <input type="text" ng-model="DelIndex">
              <button type="button" ng-click="ShwoMessage()">
              Show Message</button>
             <button type="button" ng-click="ShwoPrompt()">
             Show Prompt</button>
</div>

Output :





$filter: AngularJS filters are used for formatting data displayed
in the HTML.

Example

<!doctype html>
<html lang="en">
<head>
<script src="//code.angularjs.org/1.6.10/angular.min.js"></script>
</head>
<script>
           angular.module('myApp', [])
       .controller('myController', function ($scope, $filter) 
       {
                $scope.country = "This is a test";
                $scope.filteredText = $filter('uppercase')($scope.country);
       });

</script>
<body ng-app="myApp">
         <div ng-controller="myController">
              <span>{{filteredText}}</span><br>
         </div>
</body>
</html>

Output :
THIS IS A TEST

$locale : This service provides localization  for AngularJS components.AngularJS we always refer to it through the $locale service.

Example

<!doctype html>
<html lang="en">
<head>
<script src="//code.angularjs.org/1.6.10/angular.min.js"></script>
</head>
<script>
var app=angular.module('myApp', [])
.controller('myController', function ($scope, $locale)
         {
            $scope.localeId = $locale.id;
           $scope.CurrentTimeStap = new Date();
         });
</script>
<body ng-app="myApp">
      <div ng-controller="myController">
           <span>{{CurrentTimeStap}}</span><br>
      </div>
</body>
</html>

Ouput :

"2018-04-19T10:30:53.438Z"


$rootScope : An application has a single root scope ,Other scopes are descendant scopes of the root scope.Object added in root scope will available to all descendant scopes.AngularJS we always refer to it through the $rootScope service.

Example
<!doctype html>
<html lang="en">
<head>
<script src="//code.angularjs.org/1.6.10/angular.min.js"></script>
</head>
<script>

angular.module('myApp', []).controller('myController1', function ($scope, $rootScope)
{
         $rootScope.myValue = "Hello !";
});
angular.module('myApp').controller('myController2', function ($scope, $rootScope) {

           $rootScope.myValue = $rootScope.myValue + " I am learing";


});
angular.module('myApp').controller('myController3', function ($scope, $rootScope) {

             $scope.myValue = $rootScope.myValue + " AngularJS";


});
</script>
<body ng-app="myApp">
       <div ng-controller="myController1">
        </div>
         <div ng-controller="myController2"></div>
         <div ng-controller="myController3">
            <span>{{myValue}}</span><br>
</div>
</body>
</html>



Ouput :
Hello ! I am learing AngularJS













006 AngularJS Data Binding

AngularJS Data Binding

Data binding  is process of passing data between model and view.In AngularJS ,the HTML part of an application.Each application has its own data template , which is called data model .Both model and view under the same application follow the same data template.Here is a sample data model


var students =
         {
              Name: "Student1",
              Age: 12,
              Address: "kolkata",
          };

View is indeed HTML part of a page under an  AngularJS a application and controller.AngularJS use application data , model, to access both view and application code.You can irate through a collcetion under a view . ng-model bind with input ,select ,textarea control.

AngularJS Data Binding are two way binding.

AngularJS is two way binding, change in one part with reflect the other part of the binding.For example ,if you bind two input ,  change one input text , will automatically change the text of other input ,no need to handle event "onchange" . This happen as AjgularJs work with DOM.


Example :


<div style="background-color: #EAEAEA;">
<!DOCTYPE >
<head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>
<script>
    var mypartone = angular.module("myApp", []).controller("myController", function($scope)
                      {

                        var students =
                         {
                            Name: "Student1",
                            Age: 12,
                            Address: "kolkata",
                         };

                        $scope.student = students;
});

</script>
              <div ng-app="myApp">
                      <div ng-controller="myController">
                       <table>
                      <tr>
                     <td>
                        Name :
                     </td>
                    <td>
                      <input type="text" ng-model="student.Name" />
                     </td>
                      </tr>
                      <tr>
                      <td>
                       Age :
                      </td>
                      <td>
                          <input type="text" ng-model="student.Age" />
                     </td>
                     </tr>
                      <tr>
                      <td>
                           Address :
                      </td>
                    <td>
               <input type="text" ng-model="student.Address" />
               </td>
               </tr>
             </table>
            <br />
                <br />
                 <br />
                 <table>
                  <tr>
                 <td>
                          Name :
               </td>
               <td>
                    <input type="text" ng-model="student.Name" />
               </td>
                </tr>
                <tr>
                <td>
                          Age :
                 </td>
               <td>
                <input type="text" ng-model="student.Age" />
              </td>
              </tr>
             <tr>
            <td>
                 Address :
            </td>
               <td>
                  <input type="text" ng-model="student.Address" />
              </td>
</tr>
</table>
</div>
</div>


Live Example

 

Name :
Age :
Address :




Name :
Age :
Address :


বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...