在Angular 13中使用搜索功能的正确方法
任何人,请提供使用搜索功能的正确方法,应该在单独的组件中显示结果&用户输入的值无法刷新以进行进一步搜索。
这是搜索组件(PIC 1)
我在名为另一个组件的结果结果中渲染了搜索组件
。结果组件用于显示搜索数据。
我需要用户在搜索后输入值进一步进行搜索,
例如(pic 2)
用户输入的值在此图2中存在,
但在我的情况下,它不存在。我认为当结果组件加载时,它令人耳目一新。
我已经将数据传递给了使用单独服务的结果组件。现在,我将数据分为两个组件
,但是如何在输入框中显示。
搜索组件.html
<form [formGroup]="jobSearchForm" (ngSubmit)="jobSearchSubmit()">
<div class="input-group job-group">
<div class="input-group-btn search-panel">
<select class="form-select btn btn-default search-filter-btn" formControlName="versionId">
<option *ngFor="let version of versions" value="{{version.code}}">
{{version.Name}}
</option>
</select>
</div>
<span class="input-group-btn search-lbl">
<span>
<fa-icon [icon]="briefcase" class="job-title-ico"></fa-icon>Job Title
</span>
</span>
<input type="text" formControlName="name" class="txt-large-search"
placeholder="Example: Banking, Insurance and other Financial Clerks">
<span class="input-group-btn">
<button class="searchbtn-lg" type="submit">Search</button>
</span>
</div>
</form>
结果组件.html
<app-search-component></app-search-component>
搜索组件typescript
jobSearchSubmit() {
this.fetchedJobTitleName = this.jobSearchForm.controls.name.value;
this.fetchedVersionId = this.jobSearchForm.controls.versionId.value;
this.fetchedDatas = this.jobSearchForm.controls;
this.router.navigate(['search']);
}
以下代码用于传递数据
服务文件
jobTitleMethod: Observable<any>;
private jobTitleMethodData = new BehaviorSubject<any>('');
constructor() {
this.jobTitleMethod = this.jobTitleMethodData.asObservable();
}
jobMethod(data: any) {
this.jobTitleMethodData.next(data);
}
** search.ts **
var passingData: any = {
Name: this.fetchedJobTitleName,
versionCode: this.fetchedVersionId,
};
this.dataService.jobMethod(passingData);
结果
this.dataService.jobTitleMethod.subscribe(
(result) => {
this.searchedParams = result;
this.fetchedName = result.Name;
this.fetchedVersionId = result.versionCode;
},
(err) => {
console.warn(err);
}
);
Anyone please provide the proper way of using search functionality it should be showing results in the separate component & the user entered value could not be refreshed for further search.
This is the search Component (pic 1)
I have rendered the search component in another component named result
The result component is used for showing searched datas.
I need the user entered value after the search also to do further searches
the need like (pic 2)
The User entered Value is present in this pic 2
But in my case it is not there. i think it is refreshing when the result component loads.
I have passed data to result component using seperate service. now i have the data in two components
but how can i showed in the input box.
search component .html
<form [formGroup]="jobSearchForm" (ngSubmit)="jobSearchSubmit()">
<div class="input-group job-group">
<div class="input-group-btn search-panel">
<select class="form-select btn btn-default search-filter-btn" formControlName="versionId">
<option *ngFor="let version of versions" value="{{version.code}}">
{{version.Name}}
</option>
</select>
</div>
<span class="input-group-btn search-lbl">
<span>
<fa-icon [icon]="briefcase" class="job-title-ico"></fa-icon>Job Title
</span>
</span>
<input type="text" formControlName="name" class="txt-large-search"
placeholder="Example: Banking, Insurance and other Financial Clerks">
<span class="input-group-btn">
<button class="searchbtn-lg" type="submit">Search</button>
</span>
</div>
</form>
result component .html
<app-search-component></app-search-component>
search component typescript
jobSearchSubmit() {
this.fetchedJobTitleName = this.jobSearchForm.controls.name.value;
this.fetchedVersionId = this.jobSearchForm.controls.versionId.value;
this.fetchedDatas = this.jobSearchForm.controls;
this.router.navigate(['search']);
}
Below code is used for passing data
Service file
jobTitleMethod: Observable<any>;
private jobTitleMethodData = new BehaviorSubject<any>('');
constructor() {
this.jobTitleMethod = this.jobTitleMethodData.asObservable();
}
jobMethod(data: any) {
this.jobTitleMethodData.next(data);
}
** search.ts**
var passingData: any = {
Name: this.fetchedJobTitleName,
versionCode: this.fetchedVersionId,
};
this.dataService.jobMethod(passingData);
result.ts
this.dataService.jobTitleMethod.subscribe(
(result) => {
this.searchedParams = result;
this.fetchedName = result.Name;
this.fetchedVersionId = result.versionCode;
},
(err) => {
console.warn(err);
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
运行时
您将加载与此路线关联的组件(我猜搜索Component),并且您将无法访问此。FetchedJobtitLename,this.fetchedversionId等。
您需要在路由时传递数据。参见 https://angular.io/guide/guide/router#fetch-data-be-be----- -Navigating
when running
you will load the component associated with this route (i guess SearchComponent) and you will not be able to access this.fetchedJobTitleName, this.fetchedVersionId, etc.
You need to pass data while routing. See https://angular.io/guide/router#fetch-data-before-navigating