top of page

Retrieve SharePoint List Items Using AngularJS With Multiple Apps & Multiple Controllers


In this article you will learn how to retrieve SharePoint list items using AngularJS with multiple app directives and multiple controllers. This article mainly focuses on the angular components used in different web part zones within a single page.

Generally, we will be using one single application directive with multiple controllers. Consider a scenario where angular controllers need to be placed in different web part zones. In this case, application directive should be placed on a master page or page layout elements. But master page or page layout doesn’t support ng-app directive on their elements. So ng-app directives should be placed for each controller in their respective web parts.

So, we will see how we can retrieve data from multiple lists and display it on multiple components. In our case, we will consider how we can show the values of 2 SharePoint list's items on two different components (different web part zones) with in single 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 multiple app directive which will be defined for each components where controller is defined.

Add 2 content editor web parts to 2 different web part zones in a page. Refer the angular script file in master page or page layout or before the script loads.

For first content editor web part, declare the html as below.

  1. <div id="App1" ng-app="listApp1" ng-controller="controller1">

  2. <h1>First List Items</h1> <div ng-repeat="item in items">

  3. <p>{{item.Title}}</p>

  4. </div>

  5. </div>

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

  1. var appVar1 = angular.module('listApp1', []);       

  2. appVar1.controller("controller1", function($scope){  

  3. $.ajax({              

  4. url: "/_api/web/lists/GetByTitle('List1')/items",              

  5. method: "GET",              

  6. async: false,              

  7. headers: { "Accept": "application/json;odata=verbose" },              

  8. success: function(data){                  

  9. $scope.items = data.d.results;              

  10. },             

  11.  error: function(sender,args){                  

  12. console.log(args.get_message());              

  13. }          

  14. });      

  15. });  

For second editor web part, declare the html as below.

  1. <div id="App2" ng-app="listApp2" ng-controller="controller2">

  2. <h1>Second List Items</h1>

  3. <div ng-repeat="item in items">

  4. <p>{{item.Title}}</p>

  5. </div>

  6. </div>

Here is the trick. The app directive should be initialized using bootstrap method and the element id. The below code snippet helps you understand the logic.

  1. var appVar2 = angular.module('listApp2', []);       

  2. appVar2.controller("controller2", 

  3. function($scope){          

  4. $.ajax({             

  5.  url: "/_api/web/lists/GetByTitle('List2')/items",              

  6. method: "GET",              

  7. async: false,              

  8. headers: { "Accept": "application/json;odata=verbose" },              

  9. success: function(data){                  

  10. $scope.items = data.d.results;              

  11. },              

  12. error: function(sender,args){                  

  13. console.log(args.get_message());              

  14. }          

  15. });      

  16. });      

  17. angular.bootstrap(document.getElementById("App2"),['listApp2']);  

The below snapshot shows you web parts on different zones.

So, any number of app directives can be used in SharePoint pages. But all the app directives should be initialized using bootstrap like below. App2 denotes the element id and listApp2 denotes the app directive.

  1. angular.bootstrap(document.getElementById("App2"),['listApp2']);  


Thus you have seen how to get the values from SharePoint list using AngularJS with multiple apps and multiple controller directives. This will help developers who define the content on different web part zones where angular logic is used.

Read more articles on SharePoint:



bottom of page