首页 > 解决方案 > 如何将值传递给@Input Angular 8

问题描述

我想通过服务传递价值

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" >...

在你的组件中

@Input() search: T;

T是类型以获取更多详细信息,请查看https://angular.io/api/core/Input


推荐阅读