简体   繁体   English

如何将输入变量传递给服务 Angular

[英]How to pass input variable to a service Angular

I'm working on an Angular web project and finding it difficult to pass a user input field to service to get a request on an API endpoint.我正在处理一个 Angular Web 项目,发现很难将用户输入字段传递给服务以获取 API 端点上的请求。

so reassigning the inputKeyword in the page.component.ts to the input in the service.ts因此将 page.component.ts 中的 inputKeyword 重新分配给 service.ts 中的输入

I will need to do it the right way我需要以正确的方式去做

page.component.html页面.component.html

<input id="search" type="text"  [(ngModel)]="inputKeyword" name="search" />


 <button  (click)="searchData()"> Search </button>

page.component.ts page.component.ts

inputKeyword :  string = ''; 


 searchData(){ 
      return this.googleService.getData().subscribe(x =>{
        this.googleData= x.items;
        console.log("input ",this.inputKeyword)
        this.inputKeyword = this.googleService.input;
        console.log(this.googleData)
       }, error => {
         console.log(error)
       })

 }

data.service.ts数据服务.ts

input;

getData() {
    return   this.http.get('url?key='
     + environment.apiKey + '&cx=' + environment.cx + '&q=' + this.input)
  }

You are subscribing to the method from the service where the input property is undefined.您正在从未定义input属性的服务订阅该方法。 You assign a value only in the subscription which is wrong in this case.您只在订阅中分配一个值,在这种情况下是错误的。

Your method from the service should be like this:你的服务方法应该是这样的:

getData(input: string) {
   return   this.http.get('url?key='
    + environment.apiKey + '&cx=' + environment.cx + '&q=' + input)
 }

And in the component you pass the inputKeyword property as an argument to that method like so:在组件中,您将inputKeyword属性作为参数传递给该方法,如下所示:

searchData(){ 
      return this.googleService.getData(this.inputKeyword).subscribe(x =>{
        this.googleData= x.items;
        console.log(this.googleData)
       }, error => {
         console.log(error)
       })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将@Input中的变量传递给Angular2组件中的服务&gt; - How can I pass a variable from @Input to a service in an Angular2 component> 如何将输入标签值传递给(单击)函数并在服务角度 8 中使用 - how to pass input tag value to a (click) function and use in service angular 8 如何将服务返回的数据作为子组件的输入传递? 角度2 - How to pass service returned data as an input of child component? Angular 2 如何将数据 Input() 从组件传递到服务 class Angular - how to pass data Input() from component to service class Angular 如何在angular service.ts中传递fix变量? - How to pass fix variable in angular service.ts? 如何将服务变量传递到 Angular Material 对话框中? - How can I pass a service variable into an Angular Material dialog? Angular:如何直接从服务中操作组件中的@Input /局部变量? - Angular : How to Manipulate an @Input / local variable in component directly from a service? 无法将输入字段值传递给Angular 2中的服务 - cannot pass the input field value to service in Angular 2 有没有办法将用户输入传递给 angular 中的服务方法? - Is there a way to pass user input to service method in angular? Angular4-将变量和表单内容传递给服务 - Angular4 - Pass variable and Form Content to Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM