XML
XML
JSON
Application level
Essent modules
API Gateway
CoreModule
FeatureModule
FeatureModule
FeatureModule
SharedModule
?
const observable$ = Observable.of('foo');
observable$
.subscribe(
value => console.log(value),
error => console.error(error),
() => console.log('Completed')
);
// LOG: foo
// LOG: Completed
observable$
.map(text => text + ' bar')
.subscribe(
value => console.log(value),
error => console.error(error),
() => console.log('Completed')
);
// LOG: foo bar
// LOG: Completed
const subject = new Subject();
subject
.subscribe(
value => console.log(value),
error => console.error(error),
() => console.log('Completed')
);
subject.next('foo'); // LOG: foo
subject.next('bar'); // LOG: bar
subject.complete(); // LOG: Completed
subject.next('foo');
const subject = new Subject();
const observable$ = subject.asObservable();
observable$
.subscribe(
value => console.log(value),
error => console.error(error),
() => console.log('Completed')
);
subject.next('foo'); // LOG: foo
subject.next('bar'); // LOG: bar
observable$.next('foo'); // Error
@Injectable()
export class BudgetBillService {
amount$: Observable<number>;
private budgetBillStateSubject: Subject<BudgetBillState>;
constructor(private http: Http) {
this.budgetBillStateSubject = new Subject<BudgetBillState>();
this.amount$ = this.budgetBillStateSubject.asObservable()
.map((bbState: BudgetBillState) => bbState.amount);
}
get(): void {
this.http.get('assets/todos.json')
.map((res: Response) => res.json())
.subscribe(
(bbState: BudgetBillState) =>
this.budgetBillStateSubject.next(bbState),
(error: Response) => console.error(error),
() => console.log('Completed')
);
}
}
Hot
Cold
@Injectable()
export class TodosService {
todos$: Observable<Todo[]>;
private todosSubject: Subject<Todo[]>;
constructor(private http: Http) {
this.todosSubject = new Subject();
this.todos$ = this.todosSubject.asObservable();
}
get(): void {
this.http.get('assets/todos.json')
.map((res: Response) => res.json())
.subscribe(
(todos: Todo[]) => this.todosSubject.next(todos),
(error: Response) => console.error(error),
() => console.log('Completed')
);
}
}
Hot
@Component({
selector: 'q-todos',
templateUrl: 'todos.component.html',
styleUrls: ['todos.component.css']
})
export class TodosComponent implements OnInit {
todos: Todo[] = [];
constructor(private todosService: TodosService) {
}
ngOnInit() {
this.todosService.todos$
.subscribe(
(todos: Todo[]) => this.todos = todos
);
this.todosService.get();
}
}
const subject = new Subject();
const observable$ = subject.asObservable();
const subscription = observable$
.subscribe(value =>
console.log(value)
);
subject.next('foo'); // LOG: foo
subscription.unsubscribe();
subject.next('bar');