简体   繁体   English

AngularJS - 在指令中绑定多个类

[英]AngularJS - Binding multiple classes in directive

I'm using Waves plugin and I want to use this on 3 different classes. 我正在使用Waves插件,我想在3个不同的类上使用它。

In jQuery my codes goes like this 在jQuery中,我的代码就像这样

Waves.attach('.button', ['waves-button', 'waves-float']);
Waves.init();

In Angular I have converted this to a directive 在Angular中,我将其转换为指令

.directive('waves', function(){
        return {
            restrict: 'A',
            link: function(scope, element) {
                Waves.attach('.btn', ['waves-button', 'waves-float']);
                Waves.init();
            }
        }
    })

I have many elements with above mentioned classes and in all of them, I have to use an extra attribute to make it work data-waves 我有许多带有上述类的元素,并且在所有这些元素中,我必须使用额外的属性来使其工作data-waves

To avoid this, I've restricted the directive to class but I was able to bind only one class. 为了避免这种情况,我将指令限制为类,但我只能绑定一个类。

.directive('btn', function(){
      return {
          restrict: 'C',
          ...

How can I bind multiple classes to a single directive when it's restricted to class? 当限制为类时,如何将多个类绑定到单个指令? I'm expecting some thing like 我期待一些像

.directive(['class1', 'class2', 'class3'], function(){
      return {
         restrict: 'C',

I think that you must create new directive but share the factory function. 我认为你必须创建新指令但共享工厂功能。

function mySharedDirectiveFactory () {
  return {
  // directive object
  };
}
angular.module('myModule')
.directive('class1', mySharedDirectiveFactory)
.directive('class2', mySharedDirectiveFactory)
.directive('class3', mySharedDirectiveFactory);

Anyway I think that the best is to create just one directive and restrict it to an element or an attribute, so you will have just one definition. 无论如何,我认为最好只创建一个指令并将其限制为元素或属性,因此您将只有一个定义。

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

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