Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度2:在参数检索时未更新类属性_Javascript_Angular - Fatal编程技术网

Javascript 角度2:在参数检索时未更新类属性

Javascript 角度2:在参数检索时未更新类属性,javascript,angular,Javascript,Angular,我有这个密码 testFunction() { let params = <any>{}; if (this.searchTerm) { params.search = this.searchTerm; } // change the URL this.router.navigate(['job-search'], {queryParams: params, replaceUrl: true}); // same

我有这个密码

testFunction() {

    let params = <any>{};

    if (this.searchTerm) {
        params.search = this.searchTerm;
    }

    // change the URL
    this.router.navigate(['job-search'], {queryParams: params, replaceUrl: true});

    // same component, so just parse URL
    this.route
        .queryParams
        .subscribe(params => {
            this.searchTerm = 'search' in params ? params['search'] : '';
        });

    console.log('With Search Term: ' + this.searchTerm);

    this.jobService.search(
        this.pageNumber,
        this.pageSize,
        this.searchTerm).subscribe(
            response => {
                if (response.success) {
                    this.jobs = jobs.data;
                    this.paginatorData.total = jobs.data.total;
                    this.paginatorData.per_page = jobs.data.per_page;
                }
            }, error => {}
    );
}
testFunction(){
设params={};
如果(本搜索术语){
params.search=this.searchTerm;
}
//更改URL
this.router.navigate(['job-search'],{queryParams:params,replaceUrl:true});
//相同的组件,所以只需解析URL
这条路线
.queryParams
.订阅(参数=>{
this.searchTerm='search'在参数中?参数['search']:'';
});
console.log('With Search Term:'+this.Search Term);
这是jobService.search(
这个.页码,
这个页面大小,
这个。searchTerm)。订阅(
响应=>{
if(response.success){
this.jobs=jobs.data;
this.paginatorData.total=jobs.data.total;
this.paginatorData.per_page=jobs.data.per_page;
}
},错误=>{}
);
}
由按钮触发:

<div class="input-group input-group-sm jobs-table-search" style="width: 150px;">
                    <input id="search-box" [(ngModel)]="searchTerm" type="text" name="table_search" 
                           class="form-control pull-right" placeholder="Search" (keyup.enter)="parseRequest()">
                    <div class="input-group-btn">
                        <button type="submit" class="btn btn-default" (click)="testFunction()">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>
                </div>

我的问题是:

  • 输入“示例术语”,单击按钮,此.searchTerm不会更改
  • 再次单击按钮,此.searchTerm将更改
  • 删除“示例术语”,设置为空字符串,按按钮,此。searchTerm保留为“示例术语”
  • 再次按下按钮,此.searchTerm现在为空字符串
  • 当我在
    setTimeout


    怎么了?(很抱歉,Angular 2还是个新手)

    我想到的一件事是,在导航之后,在函数内部订阅路由的查询参数

    通常,人们会在组件的构造函数中订阅一次路由更改,并定义更改时应该发生什么


    代码的结构方式现在导致对route param的更改有多个订阅,我将首先将其移动到组件的构造函数。

    将路由订阅移动到构造函数。您在哪里声明了
    searchTerm:string