Tuesday, July 7, 2015

$document service in AngularJS

$document service provides a shortcut to access document properties in AngularJS, however this is not a direct reference to the document object, it is a reference to jQuery document property collection. The properties of the collection can be accessed using prop.

The following is the syntax to access document property collection using AngularJS
$document.prop( "<document property to access>" )

The following example shows how to access the page title of the page using $document

<html ng-app>
  <head>
    <meta charset="utf-8">
    <title>AngularJS - $document Basic</title>
    <script src="angular.min.js"></script>
    <script>
      function docController($scope, $document){
              $scope.Title = $document.prop("title");
      }
    </script>
  </head>
  <body>
          <div ng-controller="docController">
                   {{Title}}
          </div>
  </body>
</html>

Output


Search Flipkart Products:
Flipkart.com

No comments: