angular2如何在刷新页面后使用填充搜索表单获取queryparams?

时间:2017-05-16 03:20:23

标签: javascript angular typescript rxjs5

当我提交搜索表单时,网址查询参数将会更改。

当我刷新页面时,我想获取查询文件,自动填写搜索表单。

但我不希望订阅被更改,因为我想只获得一次。

这是我的搜索模型:

constructor(
    private formBuilder: FormBuilder,
    private router: Router,
    private activatedRoute: ActivatedRoute
){
    this.searchForms = this.formBuilder.group({
        cardId: [],
        cardType: [],
        beginTime: [],
        endTime: []
    });

    // this.activatedRoute.queryParams.getValue() 
    // I want use getValue method, But it throw a error msg:
    // "Property 'getValue' does not exist on type 'Observable<Params>'"
    // I do not know this method is correct.
    // Or have ohter better implementations.
    // And when i get queryparams, how to set the search model?
}

1 个答案:

答案 0 :(得分:1)

您只能使用first订阅第一个活动。并且subscribe回调只会被调用一次:

this.activatedRoute.queryParams.first().subscribe(params => {
    // do something
})