[英]Angular service pass data to component
I have search field in my top-bar component
and I need to get input value of that search field in one of my components, but it doesn't pass the value.我的
top-bar component
中有搜索字段,我需要在我的一个组件中获取该搜索字段的输入值,但它没有传递该值。
Top bar component
receive input value then pass it to Search service
Top bar component
接收输入值,然后将其传递给Search service
Result component
receive input value from Search service
and do it's process Result component
从Search service
接收输入值并执行它的过程Top bar component
Top bar component
中获取输入值Result component
Result component
中获取任何值TopBar component
// HTML
<input nz-input nzBorderless placeholder="Search here..." (keydown.enter)="onInput($event)" style="background-color: #F3F6FC;" />
// SCRIPT
import { SearchServiceService } from 'src/app/services/search/search-service.service';
@Input() searchName: string;
constructor(
private globalSearchService: SearchServiceService,
) {}
public onInput(event: any){
// this pushes the input value into the service's Observable.
this.globalSearchService.searchTerm.next(event.target.value);
}
Search service
export class SearchServiceService {
public searchTerm: BehaviorSubject<string> = new BehaviorSubject<string>(null);
constructor() { }
}
Result component
import { SearchServiceService } from 'src/app/services/search/search-service.service';
public searchTerm: string = "";
constructor(
private globalSearchService: SearchServiceService
) { }
ngOnInit(): void {
// this listens to the input value from the service and does something on change.
this.globalSearchService.searchTerm.subscribe((newValue: string) => {
console.log('search newValue :', newValue); // Nothing prints here!
// this is where you would apply your existing filtering.
this.searchTerm = newValue;
});
}
Any idea?任何想法?
Q: How did I register my search service?问:如何注册我的搜索服务?
A: A:
import { SearchServiceService } from 'src/app/services/search/search-service.service';
const MODULES = [CommonModule, RouterModule, AntdModule, TranslateModule, InfiniteScrollModule, FormsModule, ReactiveFormsModule]
@NgModule({
imports: [...MODULES],
declarations: [ACLComponent],
exports: [...MODULES],
providers: [SearchServiceService],
})
export class SharedModule {}
You're using the BehaviourSubject
wrong here.您在这里使用了错误的
BehaviourSubject
。 Implement like so.像这样实现。 also please don't expose all your variables as public and remove constructors not being used
也请不要将所有变量公开并删除未使用的构造函数
export class SearchServiceService {
searchTerm: BehaviorSubject<string> = new BehaviorSubject<string>(null);
$searchTerm = this.searchTerm.asObservable();
}
and then in your Result Component
you subscribe to the Observable
, not the BehaviourSubject
然后在您的
Result Component
中订阅Observable
,而不是BehaviourSubject
import { SearchServiceService } from 'src/app/services/search/search-service.service';
searchTerm: string = "";
constructor(
private globalSearchService: SearchServiceService
) { }
ngOnInit(): void {
this.globalSearchService.$searchTerm.subscribe((newValue: string) => {
this.searchTerm = newValue;
});
}
All I had to do was moving my service provider from shared module to app main module and rebuild (serve) my app.我所要做的就是将我的服务提供者从共享模块移动到应用程序主模块并重建(服务)我的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.