2017-08-03 75 views
0

文件结构图像Http请求不列入取输入值

enter image description here

export class FaqService { 
public inputValue; 
constructor(private http: Http) {} 

ngOnInit() {} 

setData(val) { 
this.inputValue = val; 
    console.log(this.inputValue); 
} 

getData() { 
    return this.inputValue; 
} 

getServers() { 
    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=" + this.inputValue + Staticdata.redirectUrl + "&filter="+ Staticdata.filters) 
     .map(
     (response: Response) => { 
      const items = response.json(); 
      return items; 
     }, 
    ) 
    .catch(
     (error: Response) => { 
      return Observable.throw(error); 
     } 
); 
} 
} 

在上述I类在从一个父组件的方法setData()获取数据,我不能够通过取出值为http get request

的HTTP GET请求去这样:order=desc&sort=activity&accepted=True&closed=True&title=undefined&site=stackoverflow

在标题字段它需要未定义的值。

import { Component, OnInit } from '@angular/core'; 
import { FaqService } from '../faq.service'; 

@Component({ 
selector: 'app-search-results', 
templateUrl: './search-results.component.html', 
styleUrls: ['./search-results.component.css'], 
providers: [FaqService] 
}) 

export class SearchResultsComponent implements OnInit { 
data: any[]; 
item: any[]; 

constructor(private faqService: FaqService) { 
} 
ngOnInit() { 

    this.faqService.getServers() 
     .subscribe(
     (data) => { 
      this.item = data.items; 
      console.log(this.item); 
     }, 
     (error) => console.log(error) 
    ); 
} 
} 

在这里,我调用子组件的getServers()方法。 预先感谢您。

faq.component.html

<input [(ngModel)]="inputValue" placeholder="Search FAQ" type="text"/> 
<button type="submit" id="headerSearchbutton" (click)="onRecievingResults(inputValue)"></button> 

这里我把输入使用ngModel并将该值传递给onRecievingResults功能。

onRecievingResults(value) { 
    this.faqService.setData(value); 
} 
+2

你在哪里设置数据?你在哪里叫'getServers'? – echonax

+0

你一定'getServers'将调用后'setData'称为 –

+0

不,我不知道这一点.PLS提出任何解决方案,如果u有。 @sachilaranawaka –

回答

0

@Samson,如果我理解正确的事件顺序,inputValue将在FaqService因为getServers未定义()被调用之前调用setData()被调用。该组件从其ngOnInit()方法中调用getServers(),但直到单击headerSearchbutton时它才调用setData()。

看来getServers()点是执行基于无论用户搜索已输入到现场,不是吗?如果是这样,而不是从组件的ngOnInit()中调用getServers(),为什么不在单击按钮时调用onRecievingResults?

+0

因为我想在其他组件的结果,所以我不能把它称为onRecievingResults() –