Introduction

AngularJS is perfect for Single Page Applications (SPAs) that extend HTML with new attributes. AngularJS is a JavaScript framework and it can be added to an HTML page with a <script> tag. AngularJS extends HTML attributes with Directives and binds data to HTML with Expressions. To learn more about AngularJS please refer to https://angularjs.org/.

AngularJS Directives

The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application.

The ng-model directive binds the value of the input field to the application variable name.

The ng-controller directive defines the application controller.

The $scope of the controller is the application (the HTML element) it is referred from.

AngularJS expressions are written inside double braces as in {{expression }} and they can contain literals, operators and variables.

Example

  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4.     <div ng-app="" ng-controller="controller">  
  5.         <!--ng-model binds the input field to the controller property firstName-->  
  6.         First Name: <input type="text" ng-model="user.firstName"><br>  
  7.         <!--ng-model binds the input field to the controller property lastName-->  
  8.         Last Name: <input type="text" ng-model="user.lastName"><br>  
  9.         <br>  
  10.         <!--{{}} AngularJS expression-->  
  11.         Welcome {{fullName()}}  
  12.     </div>  
  13.   
  14.     <script>  
  15.         function controller($scope) {  
  16.             // $scope.user is the property for controller object  
  17.             // firstName & lastName are the properties for user object  
  18.             $scope.user = { firstName: "", lastName: "", };  
  19.             $scope.fullName = function () {  
  20.                 var x = $scope.user;  
  21.                 return x.firstName + " " + x.lastName;  
  22.             }  
  23.         }  
  24.     </script>  
  25.     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>  
  26. </body>  
  27. </html> 

Output



In this article you will see how to use AngularJS in a SharePoint 2013 page. I have created a custom list named “Contact Details” that contains the following fields.



We will create a page with HTML form and update the preceding list item using AngularJS and the JavaScript object model.

Create a page

1. Navigate to the SharePoint site.
2. Click on Settings and then click on Add a page.



3. Enter the page name and then click on the Create button.



4. The Page is created successfully.

Use the following to add a Script Editor Webpart:

1. Navigate to the newly created page.
2. Click on the Page tab and then click on the Edit button in the ribbon interface.



3. Click on the Insert tab and then click on Web Part.



4. Select the Script Editor web part and then click on the Add button.



5. Edit the web part and then click on Edit Snippet.



6. Copy and paste the following code snippet and then click on the Insert button.

  1. <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>  
  2. <script>  
  3.     function ContactsCtrl($scope) {  
  4.   
  5.         $scope.contact = { firstName: "", lastName: "", location: "" };  
  6.         $scope.addContact = function ($event) {  
  7.             var x = $scope.contact;  
  8.             $event.preventDefault();  
  9.   
  10.             var clientContext = new SP.ClientContext.get_current();  
  11.             var web = clientContext.get_web();  
  12.             var list = web.get_lists().getByTitle('Contact Details');  
  13.   
  14.             // create the ListItemInformational object  
  15.             var listItemInfo = new SP.ListItemCreationInformation();  
  16.             // add the item to the list  
  17.             var listItem = list.addItem(listItemInfo);  
  18.             // Assign Values for fields  
  19.             listItem.set_item('Title', x.firstName);  
  20.             listItem.set_item('LastName', x.lastName);  
  21.             listItem.set_item('FullName', x.firstName + " " + x.lastName);  
  22.             listItem.set_item('Location', x.location);  
  23.   
  24.             listItem.update();  
  25.   
  26.             clientContext.executeQueryAsync(  
  27.                 Function.createDelegate(this, onQuerySucceeded),  
  28.                 Function.createDelegate(this, onQueryFailed)  
  29.             );  
  30.   
  31.         };  
  32.   
  33.         onQuerySucceeded = function () {  
  34.             alert('Successfully updated the contact');  
  35.         }  
  36.   
  37.         onQueryFailed = function (sender, args) {  
  38.             alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  
  39.         }  
  40.     }  
  41. </script>  
  42.   
  43.   
  44. <h1>Contact Information:</h1>  
  45. <br />  
  46. <div ng-app="">  
  47.     <div ng-controller="ContactsCtrl">  
  48.         <strong>First Name</strong>  
  49.         <input type="text" ng-model="contact.firstName" /><br />  
  50.         <br />  
  51.         <strong>Last Name</strong>   
  52.         <input type="text" ng-model="contact.lastName" /><br />  
  53.         <br />  
  54.         <strong>Location    </strong> <input type="text" ng-model="contact.location" /><br />  
  55.         <br />  
  56.         <input type="submit" value="Submit" ng-click="addContact($event)" />  
  57.     </div>  
  58. </div> 

7. Click on the Ok button in the web part properties window.
8. Click on the Save button in the ribbon interface.
9. You can see in the following HTML form, enter the values and then click on Submit.



10. The item is inserted successfully in the SharePoint list.



Summary

Thus in this article you saw how to use AngularJS in a SharePoint 2013 page.