|
| 1 | +/* |
| 2 | + * Angular 2 decorators and services |
| 3 | + */ |
| 4 | +import {Directive, Component, View, ElementRef} from 'angular2/angular2'; |
| 5 | +import {RouteConfig, Router} from 'angular2/router'; |
| 6 | +import {Http, Headers} from 'angular2/http'; |
| 7 | + |
| 8 | +/* |
| 9 | + * Angular Directives |
| 10 | + */ |
| 11 | +import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2'; |
| 12 | +import {ROUTER_DIRECTIVES} from 'angular2/router'; |
| 13 | + |
| 14 | + |
| 15 | +/* |
| 16 | + * App Component |
| 17 | + * Top Level Component |
| 18 | + */ |
| 19 | +@Component({ |
| 20 | + // The selector is what angular internally uses |
| 21 | + // for `document.querySelectorAll(selector)` in our index.html |
| 22 | + // where, in this case, selector is the string 'app' |
| 23 | + selector: 'app', // <app></app> |
| 24 | + // We need to tell Angular's compiler which directives are in our template. |
| 25 | + // Doing so will allow Angular to attach our behavior to an element |
| 26 | + directives: [ CORE_DIRECTIVES, FORM_DIRECTIVES, ROUTER_DIRECTIVES ], |
| 27 | + // Our list of styles in our component. We may add more to compose many styles together |
| 28 | + styles: [` |
| 29 | + .title { |
| 30 | + font-family: Arial, Helvetica, sans-serif; |
| 31 | + } |
| 32 | + main { |
| 33 | + padding: 1em; |
| 34 | + } |
| 35 | + `], |
| 36 | + // Every Angular template is first compiled by the browser before Angular runs it's compiler |
| 37 | + template: ` |
| 38 | + <header> |
| 39 | + <h1 class="title">Hello {{ title }}</h1> |
| 40 | + </header> |
| 41 | +
|
| 42 | + <main> |
| 43 | + Your Content Here |
| 44 | + <div> |
| 45 | +
|
| 46 | + <input type="text" [value]="title" (input)="title = $event.target.value" autofocus> |
| 47 | + <!-- |
| 48 | + Rather than wiring up two-way data-binding ourselves |
| 49 | + we can use Angular's [(ng-model)] syntax |
| 50 | + <input type="text" [(ng-model)]="title"> |
| 51 | + --> |
| 52 | + </div> |
| 53 | +
|
| 54 | + <pre>this.title = {{ title | json }}</pre> |
| 55 | + <pre>this.data = {{ data | json }}</pre> |
| 56 | +
|
| 57 | + </main> |
| 58 | + ` |
| 59 | +}) |
| 60 | +export class App { |
| 61 | + // These are member type |
| 62 | + title: string; |
| 63 | + data: Array<any> = []; // default data |
| 64 | + // TypeScript public modifiers |
| 65 | + constructor(public http: Http) { |
| 66 | + this.title = 'Angular 2'; |
| 67 | + } |
| 68 | + |
| 69 | + onInit() { |
| 70 | + // Our API |
| 71 | + // Before you start the app, run these commands in another process: |
| 72 | + // |
| 73 | + // - npm run express-install |
| 74 | + // - npm run express |
| 75 | + // |
| 76 | + // This will start a process that will listen for requests on port 3001 |
| 77 | + |
| 78 | + const BASE_URL = 'http://localhost:3001'; |
| 79 | + const TODO_API_URL = '/api/todos'; |
| 80 | + const JSON_HEADERS = new Headers(); |
| 81 | + |
| 82 | + JSON_HEADERS.append('Accept', 'application/json'); |
| 83 | + JSON_HEADERS.append('Content-Type', 'application/json'); |
| 84 | + |
| 85 | + this.http |
| 86 | + .get(BASE_URL + TODO_API_URL, { |
| 87 | + headers: JSON_HEADERS |
| 88 | + }) |
| 89 | + .map(res => res.json()) |
| 90 | + .subscribe( |
| 91 | + // onNext callback |
| 92 | + data => this.serverData(data), |
| 93 | + // onError callback |
| 94 | + err => this.errorMessage(err), |
| 95 | + // onComplete callback |
| 96 | + () => console.log('complete') |
| 97 | + );//end http |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + serverData(data) { |
| 102 | + console.log('data', data); |
| 103 | + this.data = data; |
| 104 | + }//serverData |
| 105 | + |
| 106 | + errorMessage(err) { |
| 107 | + console.info(`${'\n' |
| 108 | + } // You must run these commands in another process for the Http API to work ${'\n' |
| 109 | + } npm run express-install ${'\n' |
| 110 | + } npm run express |
| 111 | + `); |
| 112 | + }//errorMessage |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +/* |
| 119 | + * Please review the examples/ folder for more angular app examples |
| 120 | + * (The examples may not be updated as quickly. Please open an issue on github for us to update it) |
| 121 | + * you can change the `entry` in webpack.config to quickly view the examples |
| 122 | + * For help or questions please contact us at @AngularClass on twitter |
| 123 | + * or our chat on Slack at https://AngularClass.com/slack-join |
| 124 | + * or via chat on Gitter at https://gitter.im/AngularClass/angular2-webpack-starter |
| 125 | + */ |
0 commit comments