我目前在角2工作。在这里我正面临一个有趣的问题。我在我的标题中有一个搜索栏,它将我重定向到“resultsComponent”。在resultsComponent它显示我的搜索结果。但是当我站在resultsComponent并且想要搜索别的东西。当我在搜索栏中输入要搜索的内容时,它不会重定向我,因为我已经在该组件中。由于其中resultsComponent的构造函数不会再次命中。我无法获得查询值,因为我正在构造函数中获取查询值。我如何重新加载这个构造函数。这里是我的代码示例。 HeaderSearch如何在角度2中刷新或重新加载组件?
<input type="search" name=query [(ngModel)]="query" #quer="ngModel" class="form-control" class="form-control" required>
<button type="submit"(click)="Search()" class="btn">
<i class="icon icon-header-search"></i>
</button>
和.TS
Search(){
// alert(this.query)
//console.log(this.query);
localStorage.setItem('val',JSON.stringify(this.query));
this.router.navigate(['/result']);
}
和result.ts
constructor() {
this.query=JSON.parse(localStorage.getItem('val'));
}
this.httpService.searchGeneric(this.query,1).subscribe(
data => {
this.Getspecs = data;
this.count=data['totalItems'];
}
);
,当我站在结果组件。任何人都可以告诉我的搜索如何继续工作,并在我再次搜索时更新结果组件。任何帮助或建议是高度赞赏。
可以请你告诉我如何保持依存性构造?用简单的例子? – maadi
在你的代码中,我目前看不到需要依赖注入,但是如果你需要看看它是什么以及它是如何工作的,你可以参考angular io文档https://angular.io/guide/dependency-injection –
ok 。不知何故,我已经从标题调用函数。它正在更新我的数据集,但不更新我的DOM?你有什么想法吗? – maadi