Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# AngularJS: Quick Start
Let's create a new CiviCRM extension with an AngularJS page. It will present
a small "About Me" screen.
## Create a CiviCRM extension
First, we'll need a skeletal CiviCRM extension:
```
$ civix generate:module org.example.aboutme
Initalize module org.example.aboutme
Write org.example.aboutme/info.xml
Write org.example.aboutme/aboutme.php
Write org.example.aboutme/aboutme.civix.php
Write org.example.aboutme/LICENSE.txt
```
## Create an Angular module
Of course, AngularJS also has its own module system -- any Angular routes,
directives, or services need to live within an Angular `module`. Let's
create one.
```
$ cd org.example.aboutme
$ civix generate:angular-module
Initialize Angular module "aboutme"
Write ang/aboutme.ang.php
Write ang/aboutme.js
Write ang/aboutme.css
```
!!! tip "Tip: Angular module names"
By default, `civix` assumes that your Angular module name matches your
extension name. In this case, both are named `aboutme`. However, this
is not required -- the option `--am` can specify a different name. This
can be useful if you want to organize your code into multiple modules.
!!! note "Note: `ang/` folder"
By convention, AngularJS source code is stored in the `ang/` folder, and
each item is named after its module. The convention is discussed in
more detail in [AngularJS: File Names](/framework/angular/files.md)
The first file, `ang/aboutme.ang.php`, provides metadata for the PHP-based
file-loader, e.g.
'requires' => array('ngRoute', 'crmUi', 'crmUtil'),
'js' => array('ang/aboutme.js', 'ang/aboutme/*.js', 'ang/aboutme/*/*.js'),
'css' => array('ang/aboutme.css'),
'partials' => array('ang/aboutme'),
'settings' => array(),
);
```
The second file, `ang/aboutme.js`, provides metadata for the JS-based
Angular runtime, e.g.
!!! tip "Tip: angular.module() and CRM.angRequires()"
The list of dependencies is declared once in PHP and once in JS. To
remove this duplication, replace the array `['ngRoute', 'crmUi', 'crmUtil']`
with a function call `CRM.angRequires('aboutme')`.
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Now let's add a new Angular-based page. This page will require a `route`
with a `controller` and an HTML template. The command `civix
generate:angular-page` will create each of these:
```
$ civix generate:angular-page EditCtrl about/me
Initialize Angular page "AboutmeEditCtrl" (civicrm/a/#/about/me)
Write ang/aboutme/EditCtrl.js
Write ang/aboutme/EditCtrl.html
```
If you inspect the code, you'll find a basic AngularJS app which uses
`$routeProvider`, `angular.module(...).controller(...)`, and so on.
The generated code will display a small "About Me" screen with the current
user's first-name and last-name.
!!! tip "Tip: Flush caches _or_ enable debug mode"
By default, CiviCRM aggregates AngularJS files and caches them. You can
flush this cache manually (`cv flush`). However, it may be easier to
disable some of the aggregation/caching features by [enabling debug
mode](/dev-tools/debugging.md).
## Open the page
Finally, we'd like to take a look at this page in the web-browser.
By default, CiviCRM combines all Angular modules into one page, `civicrm/a`.
The URL of this page depends on your system configuration. Here are a few
examples:
```
# Example: Lookup the URL on Drupal 7
$ cv url 'civicrm/a/#/about/me'
"http://dmaster.l/civicrm/a/#/about/me"
# Example: Lookup the URL on WordPress
$ cv url 'civicrm/a/#/about/me'
"http://wpmaster.l/wp-admin/admin.php?page=CiviCRM&q=civicrm/a/#/about/me"
```
!!! tip "Tip: Open the browser from the command-line"
If you're developing locally on a Linux/OSX workstation, pass the
option `--open` to automatically open the page in a web browser.