Tools






How-to: Enable Multiple Angular SharePoint Web Parts on the Same Page

This blog posts describes how you can have multiple Angular.js based SharePoint web parts (referenced via a content editor web part) on the same page.  I’m calling a content editor web part (CEWP) that references JavaScript built using the Angular.js framework an “Angular Web Part.”

Angular’s bootstrap process is super easy and just about every example you find on the internets goes something like this:

<html ng-app=’myApp’>

  <blah/><blah/><blah/>

</html>

This breaks down, however, if you want to enable multiple CEWP’s representing multiple angular web parts on the same page.  Angular will only automatically bootstrap against the first ng-app directive it finds – at least as of angular version 1.3.6.  The solution is pretty simple – manually bootstrap your code instead.  The above now changes to something like this:

<body>
<d
iv id=”bootstrapHere” ng-controller=”myController as theController”>
<blah/><blah/><blah/>
</div>
</body>

<script src=”//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js”></script>

<script>
angular.bootstrap(angular.element(document.getElementById(“bootstrapHere”)),[‘myApp’]);
</script>

Basically, instead of using ng-app on the element to do your bootstrapping, you slap an ID onto that element.  Then, use the bootstrap() method on angular itself to control the bootstrapping process at run-time. I’ve tested this with three different Angular web parts on the same page and it works a charm.

</end>

undefinedSubscribe to my blog.

Follow me on Twitter at http://www.twitter.com/pagalvin

5 comments to How-to: Enable Multiple Angular SharePoint Web Parts on the Same Page

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>