如何将值传递给@Input Angular 8
我想传递服务的价值如何将值传递给@Input Angular 8,angular,Angular,我想传递服务的价值 getSearchChange(event: string): void { this.searchChange.emit(event); } 在我的组件中,我有 @Input() set search(newSearch: string) { if (newSearch !== this.query) { this.query = newSearch; } } 我的观点是 <div *ngFor="let pokemon of pokemon
getSearchChange(event: string): void {
this.searchChange.emit(event);
}
在我的组件中,我有
@Input() set search(newSearch: string) {
if (newSearch !== this.query) {
this.query = newSearch;
}
}
我的观点是
<div *ngFor="let pokemon of pokemons.results | search: query">...
。。。
但我犯了个错误
如何通过服务将值传递给“搜索”?在angular中,您需要使用输入作为属性 您需要在html中这样做
<div *ngFor="let pokemon of pokemons.results" [search]="query" >...
这里T
是类型
有关更多详细信息,请查看您可以使用行为主题推送值和消费值。比如说 在服务类中初始化一个Behavior Subject变量,并编写一个方法将值推送到该变量 服务.ts
public searchChange: BehaviorSubject<any> = new BehaviorSubject<any>(null);
getSearchChange(event: string): void {
this.searchChange.next(event);
}
constructor(private service: Service) {
this.service.searchChange.subscribe(value => {
this.emittedValue = value;
});
}
我建议您理解可观察对象和主体的概念。那么您得到的错误是什么?我不能使用这种方法,因为我使用的是过滤器的方法,所以我需要这样写:我做到了,但使用EventEmitter,我得到了值,但我想将值传递给@Input set search(value),
constructor(private service: Service) {
this.service.searchChange.subscribe(value => {
this.emittedValue = value;
});
}