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>
我的问题是:
setTimeout
怎么了?(很抱歉,Angular 2还是个新手)我想到的一件事是,在导航之后,在函数内部订阅路由的查询参数 通常,人们会在组件的构造函数中订阅一次路由更改,并定义更改时应该发生什么
代码的结构方式现在导致对route param的更改有多个订阅,我将首先将其移动到组件的构造函数。将路由订阅移动到构造函数。您在哪里声明了
searchTerm:string
?