Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Created February 26, 2013 19:44
Show Gist options
  • Select an option

  • Save sirkitree/5041484 to your computer and use it in GitHub Desktop.

Select an option

Save sirkitree/5041484 to your computer and use it in GitHub Desktop.
Quickly incorporate AngularJS into Drupal. This example pulls in the top posts from reddit and spits out the titles in a list.
  1. Download Angular lib from http://angularjs.org/ and place in your theme's js folder.
  2. Create a controller.js file in your theme's js folder.
  3. Place the controller code included in this gist.
  4. Include the angular lib and the controll file into your theme using the theme's .info file.
  5. Modify your html.tpl.php by including the ng-app property to your <html> tag.
  6. Modify your page.tpl.php by including the ng-controller="TopPostCtrl" to your main <div> tag.
  7. Check out the example page.tpl.php in this gist to see a simple example of iterating over our list.
function TopPostCtrl($scope, $http) {
$http.jsonp('http://www.reddit.com/top.json?jsonp=JSON_CALLBACK').
success(function(res) {
$scope.posts = res.data.children;
});
}
<!DOCTYPE html>
<html ng-app>
// ... typical head, body, and includes stuff
</html>
<?
...
<div id="main" ng-controller="TopPostCtrl">
...
<div>
<span>Posts:</span>
<ul class="posts">
<li ng-repeat="post in posts">
{{post.data.title}}
</li>
</ul>
</div>
...
</div>
...
scripts[] = js/angular.min.js
scripts[] = js/controller.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment