nileshkardate
nileshkardate

Reputation: 21

How to pass parameter in function call in angular 6?

freeapiservice is my web service in which I want to call a function from another class app.component in insertFormadata function I want to call a parameter from 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&[email protected]")
    }

    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 })
    }
}

app.component.ts file code here onSubmit click I have JSON.stringify(this.data.name) this value i want to pass 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;
            }
        )
    }
}

Upvotes: 1

Views: 26495

Answers (2)

Chirag Vidani
Chirag Vidani

Reputation: 2587

  1. Your service function should accept argument.
insertFormadata(data: any): Observable <any> {
  return this.httpclient.get("/api/api?type=savedata&fname=nilesh&tfvalue=js&mobile=6547896541&type_sec=1452&city=bhopal&[email protected]")
}
  1. Your app.component should pass an argument to method.
this._freeApiService.insertFormadata(this.data).subscribe(data=>{this.lstcomments=data; });

Note: Ideally for saving data, you should use http POST call.

Something like below, in freeapiservice

insertFormadata(data:any): Observable<any> {
  return this.httpclient.post("apiurl", data)
}

Upvotes: 2

Walter Luszczyk
Walter Luszczyk

Reputation: 1522

In your service method should looks like:

 insertFormadata(name: string): Observable<any>{

and you should call:

this._freeApiService.insertFormadata(this.data.name).subscribe(

More on functions and parameters you could find here

Upvotes: 3

Related Questions