First Component.ts
onRecievingResults(value){
console.log(value);
this.faqService.saveData(value);
console.log(value);
this.router.navigate(['results'], {relativeTo: this.route});}
}
In this onRecievingResults()
function, on click of a button, I am saving the input text to a method saveData()
in a service known as faqService
.
Service.ts
saveData(value){
console.log("save data function called " + value + this.sharingData.name);
this.sharingData.name;
}
getData()
{
console.log('get Data function called ');
return this.sharingData.name;
}
getServers(name){
return this.http.get(Staticdata.apiBaseUrl + "/2.2/search/advanced?key="+ Staticdata.key +"&access_token="+ Staticdata.access_token +"&/2.2/search/advanced?order=desc&sort=activity&accepted=True&closed=True&title=" + name + Staticdata.redirectUrl + "&filter="+ Staticdata.filters)
.map(
(response: Response) => {
const items = response.json();
return items;
},
)
.catch(
(error: Response) => {
return Observable.throw(error);
}
);
}
}
In this service I have 3 methods getData()
, here I get the value from the first component and store it in another method known as saveData()
.
getServers()
method is used to make Http request.
Second component.ts
export class SearchResultsComponent implements OnInit {
data: any[];
item: any[];
myName:any;
constructor(private service: FaqService) {
this.service = service;
console.log('back called');
this.myName = service.getData();
console.log(this.myName);
}
ngOnInit() {
this.service.getServers(this.myName)
.subscribe(
(data) => {
this.item= data.items;
console.log(this.item);
},
(error) => console.log(error)
);
}
}
Here in this, I call the method getData()
to get the value and get the results from the Http request.
The problem here is it takes junk value and gives the results. How to dynamically take text from inputbox and store it in service and pass it to the other component.