Hey , i have an service with function which return Observable<string[ ]> which return array of words .
And i have http-service that implement that function . How can i send get request to obtain what this function returns … thank you i hope its clear enough.
I have a vague idea of what your doing, but having more details or even code snippets always helps
I assume what you have is something like this:
@Injectable({
providedIn: 'root' // this is only here in ng6+
})
export class MyHttpService {
public getWords(): Observable<string[]> {
return this.http.get<string[]>('my/url');
}
}
The TLDR version of “getting” the data using this is the following:
- Add this class the constructor of where you want it, to leverage Angular’s DI system.
- If your service has the
providedIn: 'root'
part, then you don’t have to do anything extra. If it doesn’t add it, otherwise you will need to manually provide this service from a module.
- “Call” the method by subscribing to the data and using it how you’d like
- There are tons of variations depending on how you want to use and leverage this data that all go into different rxjs patterns. I wont go into them here, just understand anything past this simple example probably requires a different approach.
@Component({/**/})
export class MyComponent {
constructor(private myHttpService: MyHttpService) {}
ngOnInit() {
/*
* this is a simple example that can be optimized many ways depending on what you actually want to do
*/
this.myHttpService.getWords().subscribe((data) => {
console.log(data);
})
}
}
The above setup is described in detail in the docs which I highly recommend reading if your just starting out with Angular. Angular is a feature rich framework, so knowing what it can do is a great start as there are tons of already documented patterns