Integrating AngularJS
with Drupal 7
Integrating AngularJS
w/ Andrew Riley @andrewmriley
with Drupal 7
Getting to know Angles
The Concept
•
•
•
•
•

Map controllers to Blocks/Panes
Let Drupal load the code
Let other modules handler interfacing
Use AngularJS routing or don’t
Templates load in blocks for caching
Installing
• Drop angular.min.js in libraries/angular
folder
• Enable modules
– Angles Context module*
– Angles Block module
Create Your Module
•
•
•
•

Create a custom module
Put files in a subdirectory
Use the structure you want for JS file layout
Add in functions
Important Functions
• hook_angles_post_load() - Loads your app
file after all other files.
• hook_angles_controllers() - Defines
controllers.
Important Functions
• hook_angles_templates() - Defines
template files. Used for both Controller
Blocks & ng-template.
• hook_angles_require() - Loads additional
JS files (services, factories, directives, etc)
Create a Controller Block
• Select your controller
• Select your template
• Soon to be exportable
Preloading JS Blocks
• Uses defined templates
• Works with Directives
• Requires Context currently
Context
• Define the application name
• Preload a ng-template
ng-app
• Only one per page
• Use the context to add to the body tag
• If you need to add it on a different level add
it on the theme level.
• Generally not recommended to have more
than one ng-app per page.
Gotchas
Services
• Get CSRF token if writing any data.
• It is possible to send the CSRF function
without modifying much code.
CRSF Token
var addHeaders = function(token) {
$http.defaults.headers.post['X-CSRF-Token'] = token;
$http.defaults.headers.put['X-CSRF-Token'] = token;
// Delete object does not exist by default, create it
$http.defaults.headers.delete = {};
$http.defaults.headers.delete['X-CSRF-Token'] = token;
};
Time Keeping
• Watch your AngularJS -> Drupal time
saving
• avoid .getTime()
• use .toUTCString()
• PHP can read RFC-1123
• Never transfer as seconds
Angular Services VS Entities
• Remember JS AJAX request are async
• Watch your entity relationships
The Future
Panels Support
• Additional module
• Can choose context/blocks and/or Panels
Yeoman Generator
• Manually creating a custom Angles module
isn’t fun
• yo generate-angles in the works
• Creates basic angles module
Exportables
• Angles blocks are not currently exportable
• Code in the works to add to features
Questions?
Integrating AngularJS
with Drupal 7
Integrating AngularJS
w/ Andrew Riley @andrewmriley
with Drupal 7

Integrating AngularJS with Drupal 7