【问题标题】:After searching once not able to search again搜索一次后无法再次搜索
【发布时间】:2018-01-11 04:24:51
【问题描述】:

文件结构:

我有一个输入文本框,我在其中输入一些内容并将该值传递给名为 onRecievingResults() 的函数,然后使用参数将该值传递给另一个组件并发出 http 请求,如果我再次搜索我会得到结果无法得到结果。

faq.component.ts

onRecievingResults() {
    this.router.navigate(['results', this.inputValue], {relativeTo: this.route});
}

search-results.component.ts

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

inputValue;
constructor(private faqService: FaqService,private route: ActivatedRoute,) {
}
ngOnInit() {
this.route.params.subscribe(
(params : Params) => {
 this.inputValue = params["inputValue"];
    }
);
  this.faqService.getServers(this.inputValue)
        .subscribe(
        (data) => {
            this.item = data.items;
        },
        (error) => console.log(error)
    );
  }  
}

faq.service.ts

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

【问题讨论】:

  • 一个组件只会初始化一次。
  • 在你的表单控件上绑定valueChanges
  • 如何一次又一次地重新初始化它,以便我可以搜索不同的值? @Hoyen
  • 例子请@BhavikPatel
  • 你初始化它一次,只要值改变它就会触发它。就像绑定一个事件。

标签: angular parameters routing


【解决方案1】:

appcomponent.html

<app-child [items]="items"></app-child>
<form [formGroup]="searchForm"><input formControlName="search"></form>

appcomponent.ts

export class AppComponent implements OnInit  {
 searchForm: FormGroup;
 search= new FormControl();
 searchField: FormControl;
items;

constructor(private fb: FormBuilder, private service: AppService){}

     ngOnInit(){
        this.searchField = new FormControl();
        this.searchForm = this.fb.group({search: this.searchField})

               this.searchField.valueChanges
               .debounceTime(400)
               .switchMap(res =>  this.service.search(res))
              .subscribe(res => {this.items = res});}

app-child.component.html

<h3>child component Search bar values of parent component</h3>
<div *ngFor="let item of items">
      {{item}}
    </div>

app-child.component.ts

@Component({
    selector: 'app-child',
    templateUrl: './appchild.component.html'
})
export class appChildComponent {
@Input()
    items;
}

根据您的要求,关于服务参考的搜索栏,请检查 plunker

【讨论】:

  • 如果我在父组件中有输入框 faq.component.html 和子组件 search-results.component 中的结果页面,我该怎么做.html
  • 你能不能为我在@k11k2发布的上述代码做这个
【解决方案2】:

您应该订阅输入中的更改:

@Output() searchEvent: EventEmitter = new EventEmitter();

this.searchBox
  .valueChanges
  .debounceTime(200)
  .subscribe((event) => this.searchEvent.emit(event));

【讨论】:

  • 我应该在哪个组件中添加这个
  • @output 用于将数据从子组件发送到父组件,应在子组件中使用。
  • 我只想一次又一次地搜索@k11k2
  • 将它添加到您的 SearchResultsComponent。
  • this.searchBox 是什么? @SonuKapoor
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
相关资源
最近更新 更多