Studying ionic2, angular2 & typescript (a YouTube look like theme)
It is a ionic project, which means a node project, so start installing dependencies by npm install & restore plugins
- Install NPM dependencies
$ npm install- Install ionic cordova dependencies
$ ionic state restoreStart it just like a server to run in browser
$ ionic servePUG is a HTML pre-compiler, which means that it have a specific semantic to generate the final HTML files. Check it Documentation
-
app.module.ts: def the app settings (config), like: tabs (top, bottom...), hide or not titles...
-
app.component.ts: basic the same as ng-app/app.js in ionic1 (bootstrap/launch), MyApp hass a rootPage attribute
-
app.html: main/initial content loader. the ion-nav root set the initial page, it tag can have settings like switchBack
-
theme: @import the scss from pages folders into ./src/app/app.scss
-
material_icons added
-
newPage:
- Automatic by command:
$ ionic g page MyPage
- Manually:
- create the folder with it name at ./src;
- create it files name.html, name.ts, name.scss;
- create the class, with export, inside the .ts;
export class NameNewPage {}
- import it into app.module.ts;
- add to: declarations & to entryComponents
import {NameNewPage} from '../pages/name_new_page/name_new_page';
- import it into the tabs.ts:
- create a variable referencing it:
tabNRoot: any = NameNewPage;
- add it tabs.html (here the tabNRoot change state which change property val):
<ion-tab [root]="tabNRoot" tabTitle="Title" tabIcon="icon"></ion-tab>
- create a variable referencing it:
- Automatic by command:
-
Pipe and Filter Angular:
- Just the same like angular1, just the directive has changed. Angular2 Pipe Docs.
<tag *ngFor="array | filter:arg1:arg2"></tag>
- Just the same like angular1, just the directive has changed. Angular2 Pipe Docs.
-
Binding Dynamic HTML Properties & Events:
-
Dynamic attr with no overwrite warnings:
<tag [anyProperty]="classAttrO.attrORexpression"></tag>
-
Right Way to dynamic change/bind property (do not use [class]="expression", because it overwrite all classes)
<tag [class.nameClassToShowIfExpressionOK]="obj.attrORexpression"></tag>
-
Dynamic Event (use "()" intead of "[]"))
<tag (click)="componentMethod()"></tag>
-
Additional event datas
<input type="text" (keydown)="showKey($event)">
class AnyClass { showKey(event) { alert(event.keyCode); } }
-
The default Angular2 ngModel works good (notice: "()" means ngModel is an event (one-way-binding) and "[]", means it is a tag attr (the other one-way-binding)):
<input type="text" [(ngModel)]="componentClassAttr">
-
-
Dependency Injection
- Sign the Service as Injectable and define it constructor method
import {Injectable} from '@angular/core'; @Injectable() export class NameClassService { obj; getService() { return this.obj; } }
- Turn it visible adding on main.ts in provider section
import {NameClassService} from './name-class.service'; @NgModule({ providers: [NameClassService] })
- Import it on a component and create the constructor
constructor(private raceService: RaceService){}
- add it to ngOnInit
export class Component { ngOnInit() { this.races = this.raceService.getRaces(); } }
- Sign the Service as Injectable and define it constructor method
-
Http
- ensure to include http libs (main.ts)
// HttpModule already have provider http defined import {HttpModule} from '@angular/http'; @NgModule({ import: [HttpModule] })
- tell injector about http provider (app.component.ts)
- inject the http dependency into the service (service.ts)
import {Injectable} from '@angular/core'; import {CarPart} from './car-part'; import {Http} from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class DataService { constructor(private http: Http){} getCarParts() { let obervable = this.http.get(URL); // response = param var name for callback function // foreach response (response.json) // .data = the JSON attr return obervable.map(response => <CarPart[]> response.json().data); } }
- Listen data returned from the request (component.ts)
export class Component { ngOnInit() { // carParts = param var name for callback function this.dataService.getCarParts() .subscribe(carParts => this.carParts = carParts); } }
- Preventing async bug (expecting some var that is fetching from server)
.. class ... { totalCarParts() { if(Array.isArray(this.carParts)) { for ... sum += this.carPart... } } }
- ensure to include http libs (main.ts)