Retrieve SharePoint List Items Using AngularJS With single App & Multiple Controllers

Introduction:

In this article you will learn how to retrieve SharePoint list items using AngularJS with single app directive and multiple controllers. In this scenario, we will see how we can retrieve data from multiple lists using REST API and display it on multiple components in a SharePoint page. In our case, we will consider how we can show the values of 2 SharePoint list items on two different components within  asingle page.

AngularJS will help SharePoint developers to bind the data easily on SharePoint pages. With respect to angular context, the components will have the respective controllers defined. We will consider single app directives which will be initialized at the root level of the component.

Declare the html like below. Here, “listApp” is ng-app directive, which defines the application. Controller1 and controller2 are ng-controller directives which defines the controllers. Ng-repeat directive will instantiate template for item in the item collections. The template will hold the $scope object which will be referred in the script code.

  1. <div ng-app="listApp">    
  2.     <div id="App1" ng-controller="controller1">    
  3.         <h1>First List Items</h1>    
  4.         <div ng-repeat="item in items">    
  5.             <p>{{item.Title}}</p>    
  6.         </div>    
  7.     </div>    
  8.     <div id="App2" ng-controller="controller2">    
  9.         <h1>Second List Items</h1>    
  10.         <div ng-repeat="item in items">    
  11.             <p>{{item.Title}}</p>    
  12.         </div>    
  13.     </div>    
  14.         
  15. </div>    

Then initialize the module. The below snippet helps you understand how app can be initialized using the module.

  1. var appVar = angular.module('listApp', []);  

 Then initialize both controllers using the respective controller names controller1 and controller2. The below code snippet helps you understand how the controller can be initialized with controller name and also calls the respective functions to retrieve the SharePoint list items. GetListItems is the function name with parameters $scope and list name. $scope holds the necessary properties for binding the list values.

  1. appVar.controller("controller1", function($scope){    
  2.     GetListItems($scope, "List1");    
  3. });    
  4. appVar.controller("controller2", function($scope){    
  5.     GetListItems($scope, "List2");    
  6. });   

Then we will use ajax calls to retrieve the items from SharePoint list using REST API. In this example, we are going to just retrieve the list item titles without any filters and bind it to the html elements. Remember to set headers to retrieve the list item values as JSON object. The retrieved JSON can directly bind to the html using $scope using $scope.items = data.d.results. The below code helps you understand in detail.

  1. function GetListItems($scope, listName){    
  2.     $.ajax({    
  3.         url: "/_api/web/lists/GetByTitle('"+listName+"')/items",    
  4.         method: "GET",    
  5.         async: false,    
  6.         headers: { "Accept""application/json;odata=verbose" },    
  7.         success: function(data){    
  8.             $scope.items = data.d.results;    
  9.         },    
  10.         error: function(sender,args){    
  11.             console.log(args.get_message());    
  12.         }    
  13.     });    
  14. }    

The below snapshot show the data from SharePoint list.

Summary:

Thus you have seen how to retrieve and bind the values to a SharePoint page from SharePoint list using AngularJS with single app and multiple controller directives.

 
Read more articles on SharePoint: