我有一个带有自定义模块的 Drupal 站点,我用它来调用 Angular 应用程序。
这是模块代码:
/**
* Implements hook_menu().
*/
function portfolio_menu() {
$items = array();
$items['portfolio'] = array(
'access arguments' => array('access content'),
'title' => t('Portfolio'),
'page callback' => 'portfolio_page',
);
return $items;
}
/**
* Page callback: Displays Your Application on the page.
*
* @see your_application_menu()
*/
function portfolio_page() {
$path = drupal_get_path('module', 'portfolio');
drupal_add_js($path . '/lib/angular/angular.js');
drupal_add_js($path . '/lib/angular/angular-resource.js');
drupal_add_js($path . '/js/services.js');
drupal_add_js($path . '/js/app.js');
drupal_add_js($path . '/js/controllers.js');
drupal_add_js($path . '/js/filters.js');
drupal_add_css($path . '/css/app.css');
drupal_add_css($path . '/css/bootstrap.css');
return theme('portfolio');
}
function portfolio_theme($existing, $type, $theme, $path) {
$theme = array();
$theme['portfolio'] = array(
'variables' => array(),
'template' => 'portfolio',
);
return $theme;
}
应用程序.js:
angular.module('portfolio', ['tickerFilters', 'tickerServices']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/tickers', {templateUrl:'...portfolio/partials/ticker-list.html', controller: TickerListCtrl}).
otherwise({redirectTo: '/tickers'});
}]);
这是 drupal 加载的最后一个 tpl.php 文件:
<div ng-app="portfolio" ng-init="user = <? json_encode($user); ?>">
<div ng-view></div>
</div>
我的问题是如何在调用应用程序时将 drupal $user 对象传递给 angular,以便可以在 angular 应用程序中使用 $user 数据?