简体   繁体   English

捕获Click事件以传递到Angular App中的函数

[英]Capturing Click Event to Pass Into Function in Angular App

I have set up some filters a user can click on in order to filter a list display in my Angular app. 我已经设置了一些过滤器,用户可以单击这些过滤器以过滤Angular应用程序中的列表显示。 The relevant code in my view looks like this: 我认为相关代码如下:

<md-checkbox *ngFor="let option of categoryFilters.options" [(ngModel)]="option.value" (click)="filterByCategory($event)">
     {{option.label}}
</md-checkbox>

You'll notice that I have a "filterByCategory()" function above. 您会注意到我上面有一个“ filterByCategory()”函数。 What I want to do with that is filter the list according to the value that the user clicks on from the md-checkbox list. 我要做的是根据用户从md-checkbox列表中单击的值来过滤列表。 So far I haven't been able to capture that value. 到目前为止,我还无法获得该价值。

I've tried this in my component: 我已经在组件中尝试过了:

public filterByCategory(option) {
    console.log('Category filter ' + option + ' was clicked');
}

But that just prints the mouse event to the console: 但这只是将鼠标事件打印到控制台:

'Category filter [object MouseEvent] was clicked' “单击了类别过滤器[对象MouseEvent]”

I also tried using the two-way-bound value, like this: 我也尝试使用双向限制值,如下所示:

public filterByCategory(option) {
    console.log('Category filter ' + this.option.value + ' was clicked');
}

But that returns 'undefined'. 但这返回“未定义”。

By the way, the options I'm trying access the values of here look like this in my component: 顺便说一句,我正在尝试访问此处的值的选项在我的组件中看起来像这样:

private categoryFilters = {
    enabled: true,
    options: [
        { toolbarLabel: 'A', label: 'Option A', value: false },
        { toolbarLabel: 'B', label: 'Option B', value: false },
        { toolbarLabel: 'C ', label: 'Option C ', value: false }
    ],
    text: null
};

What can use to actually get the value that was clicked on, in order to pass that into the filter function? 什么可以用来实际获取被单击的值,以将其传递给过滤器函数?

Try this. 尝试这个。 (click)="optionClicked(option)">

optionClicked(option) {
console.log(option.value)
}

UPDATE: 更新:

You can also try this: 您也可以尝试以下操作:

<md-checkbox *ngFor="let option of categoryFilters.options" 
     [(ngModel)]="option.value" (change)="filterByCategory(option.label, $event)">
   {{option.label}}
</md-checkbox>

filterByCategory(value, checked) {
  if(checked) {
    console.log(value)
  }
}

Original answer: 原始答案:

You could do it like this: I removed the two-way-binding altogether, since we are using $event and value -attribute, so in this case ngModel is redundant. 您可以这样做:我完全删除了双向绑定,因为我们使用的是$eventvalue -attribute,所以在这种情况下ngModel是多余的。 I also use change -event instead of click . 我还使用change event而不是click So change your template to: 因此,将模板更改为:

<md-checkbox *ngFor="let option of categoryFilters.options" 
    value="{{option.label}}" (change)="filterByCategory($event)">
       {{option.label}}
</md-checkbox>

And in the component, let's see if the checkbox is checked before actually looking at the option value. 在组件中,让我们先看看是否已选中该复选框,然后再实际查看选项值。

filterByCategory(event) {
  if(event.checked) {
    console.log(event.source.value)
  }
}

This seems to work fine: 这似乎工作正常:

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM