One framework.
Mobile and desktop. "
// app.modules.ts
@NgModule({
imports: [
BrowserModule,
HttpModule,
FormsModule,
routing
],
declarations: [
AppComponent,
HomeComponent,
AboutComponent
],
providers: [ApiService],
bootstrap: [AppComponent]
})
export class AppModule {}
...// login.ts
import { Component, Inject } from 'angular2/core';
import { User } from '../../services/user';
import { UserApi } from '../../lib/lb-services';
@Component({
selector: 'login',
templateUrl: 'src/components/login/login.html',
styleUrls: ['src/components/login/login.css']
})
...// login.ts
...
export class Login {
private email: string;
private password: string;
constructor(@Inject(User) public user: User,
@Inject(UserApi) public userApi: UserApi) {
}
public login() {
this.userApi.login({email: this.email, password: this.password}).subscribe(
(response: any) => { this.user.user = response.user; },
(error: any) => { this.user.clearUser();
console.error('login KO', error); },
() => { console.log('Login COMPLETE', this.user); }
);
}
public logout() {
this.userApi.logout().subscribe(
(response: any) => { this.user.clearUser(); },
(error: any) => { this.user.clearUser();
console.log('Logout KO'); },
() => { console.log('Logout COMPLETE'); }
);
}
}<!-- login.html -->
<input *ngIf="!user.user"
[(ngModel)]="email"
placeholder="Email">
<input *ngIf="!user.user"
[(ngModel)]="password"
placeholder="Password">
<button *ngIf="!user.user"
(click)="login()">LOGIN</button>
<button *ngIf="user.user"
(click)="logout()">LOGOUT</button>
<p *ngIf="user.user">
Welcome {{user.user.nome}} {{user.user.cognome}}!
</p><!-- items.html -->
<input type="button" value="Load items" (click)="getItems()">
<p>{{ text }}</p>
<table class="table" *ngIf="items.length">
<thead><tr><td>ID</td><td>NAME</td><td>DESC</td></tr></thead>
<tbody>
<tr *ngFor="#item of items">
<td>
{{ item.id }}
</td>
<td>
{{ item.name }}
</td>
<td>
{{ item.desc }}
</td>
</tr>
</tbody>
</table>import { Injectable } from 'angular2/core';
import { UserModel } from '../models/user';
@Injectable()
export class User {
private _user: UserModel;
constructor() {
}
set user(user: UserModel) {
this._user = user;
}
get user() {
return this._user;
}
clearUser() {
this._user = undefined;
}
}// login.ts
export class Login {
@Input() input: string;
printInput() {
console.log(this.input);
}
}<!-- header.html -->
...
<login [input]="parameter"></login>
...// login.ts
export class Login {
@Output() logged: EventEmitter<any>
= new EventEmitter();
}<!-- header.html -->
...
<login (logged)="loggedin($event)">
</login>
...Predictable state container for JavaScript apps"
In complex enterprise application the following problems will emerge soon or after if we not prevent them
In complex enterprise application the following problems will emerge soon or after if we not prevent them
Redux can help us to simplify design and implementation workflow
interface Action {
type: string,
payload?: any
}interface Reducer<T> {
(state: T, action: Action) : T;
}function(state: T, action?: Action) : T {
return state;
}RxJS powered state management inspired by Redux for Angular 2 apps"
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const RESET = 'RESET';
const INITIAL_STATE = Immutable.from({counter: 0, error: null});
export const counterReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case 'INCREMENT':
if (state.counter > 10)
return state.set('error', 'OVER 10!!!!1!!!1!!');
return state.set('counter', state.get('counter') + 1);
case 'DECREMENT':
return state.set('counter', state.get('counter') - 1);
case 'RESET':
return state.set('counter', 0);
default:
return state;
}
};reducer.ts
export class MyComponent {
state: Observable<State>;
constructor(public store: Store<State>) {
this.state
= <Observable<State>>store.select('counter');
}
increment() {
this.store.dispatch({ type: INCREMENT });
}
decrement() {
this.store.dispatch({ type: DECREMENT });
}
reset() {
this.store.dispatch({ type: RESET });
}
}component.ts
<button (click)="increment()">
Increment
</button>
<span>
Current Count: {{ state.pluck('counter') | async }}
</span>
<span *ngIf="(state | async).error">
Current Error: {{ state.pluck('error') | async }}
</span>
<button (click)="decrement()">
Decrement
</button>component.html