Welcome to freeapiservice, my unique web service where I aim to invoke a function from another class named app.component
within the insertFormadata
function. The goal is to call a parameter from the same app.component
:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpParams } from '@angular/common/http';
@Injectable()
export class freeApiService {
constructor(private httpclient: HttpClient) {
}
insertFormadata(): Observable<any> {
return this.httpclient.get("/api/api?type=savedata&fname=nilesh&tfvalue=js&mobile=6547896541&type_sec=1452&city=bhopal&<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0959d91999ccd9e9b918294918495b0979d91999cde939f9d">[email protected]</a>")
}
getcomments(): Observable<any> {
return this.httpclient.get("http://jsonplaceholder.typicode.com/posts/1/comments")
}
getcommentsbyparameters(): Observable<any> {
let params1 = new HttpParams().set("userId", "1");
return this.httpclient.get("http://jsonplaceholder.typicode.com/posts", { params: params1 })
}
}
In the app.component.ts
file, when the onSubmit
button is clicked, I have included JSON.stringify(this.data.name)
. This value needs to be passed to the webservice API.
import { Component } from '@angular/core';
import { freeApiService } from './services/freeapi.service';
import { Comments } from './classes/comments';
import { from } from 'rxjs';
import { Posts } from './classes/posts';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'applykaroo';
data: any = {};
onSubmit() {
alert(JSON.stringify(this.data.name));
this._freeApiService.insertFormadata().subscribe(
data => {
this.lstcomments = data;
}
);
}
constructor(private _freeApiService: freeApiService) {
}
lstcomments: Comments[];
lstparacomments: Posts[];
ngOnInit() {
this._freeApiService.getcomments().subscribe(
data => {
this.lstcomments = data;
}
);
this._freeApiService.getcommentsbyparameters().subscribe(
data => {
this.lstparacomments = data;
}
)
}
}