|
| 1 | +# loopback-example-angular |
| 2 | + |
| 3 | +**⚠️ This LoopBack 3 example project is no longer maintained. Please refer to [LoopBack 4 Examples](https://loopback.io/doc/en/lb4/Examples.html) instead. ⚠️** |
| 4 | + |
| 5 | +``` |
| 6 | +$ git clone https://github.com/strongloop/loopback-example-angular.git |
| 7 | +$ cd loopback-example-angular |
| 8 | +$ npm install |
| 9 | +$ node . # then browse to localhost:3000 |
| 10 | +``` |
| 11 | + |
| 12 | +A simple todo list using AngularJS on the client-side and LoopBack on the |
| 13 | +server-side. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +### Tutorials |
| 18 | + |
| 19 | +- [Getting started with LoopBack](https://github.com/strongloop/loopback-getting-started) |
| 20 | +- [Tutorial series, step 1](https://github.com/strongloop/loopback-example#the-basics) |
| 21 | + |
| 22 | +### Knowledge of |
| 23 | + |
| 24 | +- [Angular](https://angularjs.org/) |
| 25 | +- [Angular Resource](https://docs.angularjs.org/api/ngResource/service/$resource) |
| 26 | +- [AngularUI Router](https://github.com/angular-ui/ui-router) |
| 27 | +- [Bootstrap](http://getbootstrap.com/) |
| 28 | +- [Bower](http://bower.io/) |
| 29 | +- [LoopBack](http://loopback.io/) |
| 30 | +- [LoopBack AngularJS SDK](http://loopback.io/doc/en/lb2/AngularJS-JavaScript-SDK.html) |
| 31 | +- [LoopBack models](http://loopback.io/doc/en/lb2/Defining-models.html) |
| 32 | +- [LoopBack middleware](http://loopback.io/doc/en/lb2/Defining-middleware.html) |
| 33 | + |
| 34 | +## Procedure |
| 35 | + |
| 36 | +### Create the application |
| 37 | + |
| 38 | +#### Application information |
| 39 | + |
| 40 | +- Name: `loopback-example-angular` |
| 41 | +- Directory to contain the project: `loopback-example-angular` |
| 42 | + |
| 43 | +``` |
| 44 | +$ slc loopback loopback-example-angular |
| 45 | +... # follow the prompts |
| 46 | +$ cd loopback-example-angular |
| 47 | +``` |
| 48 | + |
| 49 | +### Create the `Todo` model |
| 50 | + |
| 51 | +#### Model information |
| 52 | + |
| 53 | +- Name: `Todo` |
| 54 | + - Data source: `db (memory)` |
| 55 | + - Base class: `PersistedModel` |
| 56 | + - Expose over REST: `Yes` |
| 57 | + - Custom plural form: *Leave blank* |
| 58 | + - Properties: |
| 59 | + - `content` |
| 60 | + - String |
| 61 | + - Required |
| 62 | + |
| 63 | +``` |
| 64 | +$ slc loopback:model Todo |
| 65 | +... # follow the prompts |
| 66 | +``` |
| 67 | + |
| 68 | +### Configure the vendor directory |
| 69 | + |
| 70 | +In the project root, create [`.bowerrc`](https://github.com/strongloop/loopback-example-angular/blob/master/.bowerrc). |
| 71 | + |
| 72 | +>Bower installs packages in `bower_components` by default, but we reconfigure |
| 73 | +this to `client/vendor` to make [importing files into `index.html`](https://github.com/strongloop/loopback-example-angular/blob/master/client/index.html#L33-L37) |
| 74 | +more convenient. |
| 75 | + |
| 76 | +### Install client-side dependencies |
| 77 | + |
| 78 | +From the project root, run: |
| 79 | + |
| 80 | +``` |
| 81 | +$ bower install angular angular-resource angular-ui-router bootstrap |
| 82 | +``` |
| 83 | + |
| 84 | +### Create the home page |
| 85 | + |
| 86 | +Create [`index.html`](https://github.com/strongloop/loopback-example-angular/blob/master/client/index.html) in the [`client`](https://github.com/strongloop/loopback-example-angular/blob/master/client) directory. |
| 87 | + |
| 88 | +### Create the main stylesheet |
| 89 | + |
| 90 | +Create a [`css` directory](https://github.com/strongloop/loopback-example-angular/blob/master/client/css) to store stylesheets. |
| 91 | + |
| 92 | +``` |
| 93 | +$ mkdir client/css |
| 94 | +``` |
| 95 | + |
| 96 | +In this directory, create [`styles.css`](https://github.com/strongloop/loopback-example-angular/blob/master/client/css/styles.css). |
| 97 | + |
| 98 | +### Serve static assets from the `client` directory |
| 99 | + |
| 100 | +Delete `/server/boot/root.js`. |
| 101 | + |
| 102 | +Add static middleware to the [`files` section in `middleware.json`](https://github.com/strongloop/loopback-example-angular/blob/master/server/middleware.json#L23-L27) |
| 103 | +. |
| 104 | + |
| 105 | +### Create `app.js` |
| 106 | + |
| 107 | +Create a [`js` directory](https://github.com/strongloop/loopback-example-angular/blob/master/client/js) to hold scripts files. |
| 108 | + |
| 109 | +``` |
| 110 | +$ mkdir client/js |
| 111 | +``` |
| 112 | + |
| 113 | +In this directory, create [`app.js`](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/app.js). |
| 114 | + |
| 115 | +>Notice we declare [`lbServices`](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/app.js#L3) as a dependency. We |
| 116 | +will generate this file using the `lb-ng` command in a later step. |
| 117 | + |
| 118 | +### Create `todo.html` |
| 119 | + |
| 120 | +Create a [`views`](https://github.com/strongloop/loopback-example-angular/blob/master/client/views) to store view templates. |
| 121 | + |
| 122 | +``` |
| 123 | +$ mkdir client/views |
| 124 | +``` |
| 125 | + |
| 126 | +In this directory, create [`todo.html`](https://github.com/strongloop/loopback-example-angular/blob/master/client/views/todo.html). |
| 127 | + |
| 128 | +### Create `controllers.js` |
| 129 | + |
| 130 | +Create a [`controllers`](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/controllers) directory to store controller |
| 131 | +files. |
| 132 | + |
| 133 | +``` |
| 134 | +$ mkdir client/js/controllers |
| 135 | +``` |
| 136 | + |
| 137 | +In this directory, create [`todo.js`](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/controllers/todo.js). |
| 138 | + |
| 139 | +### Generate `lb-services.js` |
| 140 | + |
| 141 | +Create a [`services` directory](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/services) to store service files. |
| 142 | + |
| 143 | +``` |
| 144 | +$ mkdir client/js/services |
| 145 | +``` |
| 146 | + |
| 147 | +`lb-ng` is automatically installed along with the `slc` command-line tool (ie. |
| 148 | +when you ran `npm install -g strongloop`). `lb-ng` takes two arguments: the |
| 149 | +path to [`server.js`](https://github.com/strongloop/loopback-example-angular/blob/master/server/server.js) and the path |
| 150 | +to the [generated services file](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/services/lb-services.js). |
| 151 | +[`lb-services.js`](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/services/lb-services.js) is an Angular service |
| 152 | +used to interact with LoopBack models. |
| 153 | + |
| 154 | +Create [`lb-services.js`](https://github.com/strongloop/loopback-example-angular/blob/master/client/js/services/lb-services.js) using the `lb-ng` |
| 155 | +command. |
| 156 | + |
| 157 | +``` |
| 158 | +$ lb-ng server/server.js client/js/services/lb-services.js |
| 159 | +``` |
| 160 | + |
| 161 | +### Run the application |
| 162 | + |
| 163 | +From the project root, enter `node .` and browse to [`localhost:3000`](http://localhost:3000) |
| 164 | +to view the application. |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +[More LoopBack examples](https://loopback.io/doc/en/lb3/Tutorials-and-examples.html) |
0 commit comments