Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将值传递给@Input Angular 8_Angular - Fatal编程技术网

如何将值传递给@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;
        });
    }