Angular 角度7:如何在不单击的情况下调用方法?

Angular 角度7:如何在不单击的情况下调用方法?,angular,filter,angular-components,angular-template,Angular,Filter,Angular Components,Angular Template,我觉得我没有看到什么明显的东西,但是我想不出来。。 我有一个布局组件,它显示一组列表,每个列表都有自己的标题: <h1>Vegetables</h1> <food-list-component></food-list-component> <h1>Fruit</h1> <food-list-component></food-list-component> <h1>Sweets</

我觉得我没有看到什么明显的东西,但是我想不出来。。 我有一个布局组件,它显示一组列表,每个列表都有自己的标题:

<h1>Vegetables</h1>
<food-list-component></food-list-component>

<h1>Fruit</h1>
<food-list-component></food-list-component>

<h1>Sweets</h1>
<food-list-component></food-list-component>
蔬菜
果
糖果

在食物列表组件中,我调用了一个简单的过滤方法,该方法接受参数(对于蔬菜/水果/糖果也是
食物类型
)。由于在应用程序的其他地方,我根据一些事件(单击或输入)调用此筛选方法,因此我不知道如何在这个静态布局组件中筛选这些列表,它只显示列表。

据我所知,所有
食物列表组件
实例都是相同的,并且具有相同的数据。您需要使用不同的过滤器(蔬菜、水果等)过滤每个实例

食物列表组件中的过滤器属性定义为@Input。然后将所需的过滤器从html传递到每个实例

export class FoodListComponent implements OnInit {

  @Input() filter: string;

  constructor() { }

  ngOnInit() {
    // Fetch items to be displayed in list
    // Filter the list using `filter`
  }

}
然后可以修改html以通过每个实例的过滤器

<h1>Vegetables</h1>
<food-list-component filter="vegetables"></food-list-component>

<h1>Fruit</h1>
<food-list-component filter="fruits"></food-list-component>

<h1>Sweets</h1>
<food-list-component filter="sweets"></food-list-component>
蔬菜
果
糖果

据我所知,所有
食品清单组件
实例都是相同的,并且具有相同的数据。您需要使用不同的过滤器(蔬菜、水果等)过滤每个实例

食物列表组件中的过滤器属性定义为@Input。然后将所需的过滤器从html传递到每个实例

export class FoodListComponent implements OnInit {

  @Input() filter: string;

  constructor() { }

  ngOnInit() {
    // Fetch items to be displayed in list
    // Filter the list using `filter`
  }

}
然后可以修改html以通过每个实例的过滤器

<h1>Vegetables</h1>
<food-list-component filter="vegetables"></food-list-component>

<h1>Fruit</h1>
<food-list-component filter="fruits"></food-list-component>

<h1>Sweets</h1>
<food-list-component filter="sweets"></food-list-component>
蔬菜
果
糖果

您能否给出一个调用filter方法的示例?我不清楚参数。如果要无事件筛选?您预计什么时候进行筛选?例如,如果您有一个事件
(单击)=“onFilter($event)”
。。您应该能够通过编程调用同一事件<代码>此.onFilter(事件)
。然而,这个问题似乎需要更多的澄清。是的,我正在全局搜索-filter方法位于服务内部(可观察到,可进行http调用),并使用rxjs主题作为参数。@Kris Hollenbeck布局页面只是为了显示,我希望一直对其进行过滤。这是一个初学者的问题(至少可以这么说)。@ConnorsFan类似于(不幸的是,我手头没有):
getFilteredFoodItems(蔬菜、绿色等)
你能举一个调用filter方法的例子吗?我不清楚参数。如果要无事件筛选?您预计什么时候进行筛选?例如,如果您有一个事件
(单击)=“onFilter($event)”
。。您应该能够通过编程调用同一事件<代码>此.onFilter(事件)
。然而,这个问题似乎需要更多的澄清。是的,我正在全局搜索-filter方法位于服务内部(可观察到,可进行http调用),并使用rxjs主题作为参数。@Kris Hollenbeck布局页面只是为了显示,我希望一直对其进行过滤。这是一个初学者的问题(至少可以这么说)。@ConnorsFan类似于(不幸的是,我手头没有):
getFilteredFoodItems(蔬菜、绿色等)