简体   繁体   English

使用Angular指令将可切换按钮添加到DOM

[英]Adding toggleable buttons to DOM using Angular directive

See the following Plunker: 请参阅以下柱塞:

https://plnkr.co/edit/gLFw26zoJeaM5qtPjCFg?p=preview https://plnkr.co/edit/gLFw26zoJeaM5qtPjCFg?p=preview

I have an element <sc-chart type="approved"></sc-chart> that hooks into the scChart directive to add a chart to the page (I've omitted that part of the code since that's not pertinent to this issue). 我有一个<sc-chart type="approved"></sc-chart>元素,该元素挂接到scChart指令中,以将图表添加到页面中(由于与该问题无关,我省略了该部分代码)。 After adding the chart I then want to create a group of buttons next to the chart populated with the names array (this array will not be static, it will be the result of a service call, so I can't hardcode the buttons). 添加图表后,我接下来要在填充有名称数组的图表旁边创建一组按钮(此数组不是静态的,这是服务调用的结果,因此我无法对按钮进行硬编码)。

I'm able to add the buttons okay and make them clickable. 我可以添加按钮,并使它们可单击。 I'm even able to highlight them when they're selected. 选择它们后,我什至可以突出显示它们。 However, I'm having a hard time figuring out how/when to remove the "active" CSS class, when I've clicked away from the button. 但是,在单击按钮时,我很难弄清楚如何/何时删除“活动” CSS类。 It may be something pretty simple I'm missing. 这可能是我所缺少的非常简单的东西。

just add removeClass() function: 只需添加removeClass()函数:

btn.on('click', {name: i}, function(event) {
  $('.pill-btn').removeClass('active');
  $(this).addClass('active');
  alert(event.data.name);
});

plunker: https://plnkr.co/edit/6Nj4beNuJf1plZmPAs0z?p=preview 朋克: https ://plnkr.co/edit/6Nj4beNuJf1plZmPAs0z ? p = preview

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

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