簡體   English   中英

AngularJS - 在指令中綁定多個類

[英]AngularJS - Binding multiple classes in directive

我正在使用Waves插件,我想在3個不同的類上使用它。

在jQuery中,我的代碼就像這樣

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

在Angular中,我將其轉換為指令

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

我有許多帶有上述類的元素,並且在所有這些元素中,我必須使用額外的屬性來使其工作data-waves

為了避免這種情況,我將指令限制為類,但我只能綁定一個類。

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

當限制為類時,如何將多個類綁定到單個指令? 我期待一些像

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

我認為你必須創建新指令但共享工廠功能。

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

無論如何,我認為最好只創建一個指令並將其限制為元素或屬性,因此您將只有一個定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM