A quick tip that helps me remember what they do -
HostBinding('value') myValue; is exactly the same as [value]="myValue"
And
HostListener('click') myClick(){ } is exactly the same as (click)="myClick()"
HostBinding('value') myValue; is exactly the same as [value]="myValue"HostListener('click') myClick(){ } is exactly the same as (click)="myClick()"
@Component or @Directive decorator<my-component #cmp></my-component>
with @ViewChild('cmp'))@ViewChild(SomeService) someService: SomeService){ static: true } ?
import { Observable } from "rxjs/Observable";
var observable = Observable.create((observer:any) => {
observer.next('Hey guys!')
})
Above, you can see that we're defining the subscribe function, and we're
emitting a single value of 'Hey guys!' by calling observer.next().
var observable = Observable.create((observer:any) => {
observer.next('Hey guys!')
})
observable.subscribe((x:any) => console.log(x));
Please see the reference....
REF:
https://coursetro.com/posts/code/148/RxJS-Observables-Tutorial---Creating-&-Subscribing-to-Observables
https://www.learnrxjs.io/operators/creation/create.html
You can use these creation operators that create observables in a variety of ways:
unsubscribe, that takes no argument and just disposes the resource held by the subscription.HttpClient, you need to import the Angular HttpClientModule.
Most apps do so in the root AppModule.import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
}
this.http.get/this.http.post returnObservable
so to get the data write code somthing like
this.configService.getConfig()
.subscribe((data: Config) => this.config = {
heroesUrl: data['heroesUrl'],
textfile: data['textfile']
})
Try to use combineLatest instead of forkJoin :
With combineLatest :
const combined = Observable.combineLatest(
this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)
combined.subscribe(latestValues => {
const [ data_changes , data_all ] = latestValues;
console.log( "data_changes" , data_changes);
console.log( "data_all" , data_all);
});You can also handle it by forkJoin , but forkJoin will return data when all calls are finished and return result , but in combineLatest When any observable emits a value, emit the latest value from each.
With forkJoin :
const combined = Observable.forkJoin(
this.http.get('https://testdb1.firebaseio.com/.json').map((res: Response) => res.json()),
this.http.get('https://testdb2.firebaseio.com/.json').map((res: Response) => res.json())
)
combined.subscribe(latestValues => {
const [ data_changes , data_all ] = latestValues;
console.log( "data_changes" , data_changes);
console.log( "data_all" , data_all);
});Call both and check the console log , you will get idea.
export class App {
items: Array<string>;
term = new FormControl();
constructor(private wikipediaService: WikipediaService) {
this.term.valueChanges
.debounceTime(400)
.subscribe(term => this.wikipediaService.search(term).then(items => this.items = items));
}
}toPromise() // use obserable to promise() // I used in serverless async and await in s3 sdkreturn this.jsonp
.get('http://en.wikipedia.org/w/api.php?callback=JSONP_CALLBACK', { search })
.toPromise()
.then((response) => response.json()[1]);
fromPromise() /// it use in obserable from promiseAutoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper cl...