Introduction to AngularJS in SharePoint 2013

We can try some basic stuff in SharePoint using AngularJS. Now a days AngularJs is becoming popular among web development to do client-side jobs.

It's a very handy JavaScript framework to represent the data with DOM in the HTML structure.

To learn more about AngularJs, and there are many videos and articles across the globe.

We will now return to our show on using the AngularJS on the SharePoint business.

The example uses the SharePoint REST API to fetch the sub-site details and using AngularJs, we will render in a simple table without much code.


AngularJS file

The latest stable version of AngularJS available from the following location:
SharePoint REST API enabled environment

HTML Snippet
  1. <style type="text/css">  
  2. .web-table th{  background-color:#ddd;  border:2px solid #fff;}  
  3. .web-table td{  background-color:#eee;  border:2px solid #fff;}  
  4. .web-heading{   padding:2px;}  
  5. </style>  
  6. <script type="text/javascript" src="/siteassets/angular.min.js"></script>  
  7. <div ng-app="spng-App">  
  8. <h2 class="web-heading">Sub Sites</h2>  
  9. <div ng-controller="spng-WebCtrl">  
  10.     <table width="100%" cellpadding="10" cellspacing="2" class="web-table">  
  11.     <thead>  
  12.     <tr>  
  13.         <th>Title</th>  
  14.         <th>Id</th>  
  15.         <th>Created</th>  
  16.         <th>Web Template</th>  
  17.     </tr>  
  18.     </thead>  
  19.     <tbody>  
  20.     <tr ng-repeat="_web in Webs">  
  21.         <td>{{_web.Title}}</td>  
  22.         <td>{{_web.Id}}</td>  
  23.         <td>{{_web.Created | date:'medium'}}</td>  
  24.         <td>{{_web.WebTemplate}}</td>  
  25.     </tr>  
  26.     </tbody>  
  27.     </table>  
  28. </div>  
  29. </div> 
JavaScript Snippet
  1. <script type="text/javascript">  
  2. var spApp= angular.module('spng-App', []);  
  3. spApp.controller('spng-WebCtrl'function($scope, $http)
  4.     $http(
  5.         method: "GET",  
  6.         url:"https://sharepointsite/_api/web/webs",  
  7.         headers: {"Accept":"application/json;odata=verbose"}  
  8.     }  
  9.     ).success(function(data, status, headers, config)
  10.         $scope.Webs = data.d.results;  
  11.       }).error(function (data, status, headers, config)
  12.       });  
  13. });  
  14. </script> 
I have download the angularJs file and uploaded that file to SiteAssets. Instead, we can directly refer to the CDN:
  1. <script id="angularScript" src="//"></script> 
  • ng-app attribute to define the root element to auto-bootstrap an application
  • ng-controller attribute attaches the controller to the view, the controller has a method to call the REST API to fetch the results.
  • ng-repeat attribute in the tr tag is an Angular repeater directive. The repeater tells Angular to create a tr element for each web (_web) in the array using the <tr> … </tr> tag as the template.
  • _web.Title, _web.Id, _web.Created, _web.WebTemplate return the actual value from the REST API.
We can insert the code to the Content Editor / Script Editor Web part to have the following screenshot.

That shows the sub sites under the site https://sharepointsite