2015-01-26 31 views
0

在一個html表中,我有一個使用ng-repeat構造的行的列表。在這些行內我有一個單選按鈕,它使用jquery來改變樣式。風格是通過使用這個jQuery應用...我應該把這個jQuery內部的角度控制器?

<script> 
$(document).ready(function() { 
$('.i-checks').iCheck({ 
checkboxClass: 'icheckbox_square-green', 
radioClass: 'iradio_square-green' 
}); 
}); 
</script> 

當我把在頁面的代碼,單選按鈕造型不起作用。但是,如果我把代碼中的錶行中 - 它的工作...

<div ng-controller="supplierController" class="row"> 
<div class="col-lg-6 m-t-m"> 
<div class="ibox float-e-margins"> 
    <div class="ibox-title"> 
     <h5>Price results </h5> 
    </div> 
    <div class="ibox-content"> 

     <table class="table table-striped"> 
      <thead> 
      <tr> 
       <th>Vendor</th> 
       <th>Details</th> 
       <th></th> 
      </tr> 
      </thead> 
      <tbody> 
      <tr ng-repeat="supplier in suppliers.prices"> 
       <td>{{ supplier.supplier_name }}</td> 
       <td>£{{ supplier.price }}</td> 
       <td> 
        <div class="radio i-checks"><label> <input 
type="radio" name="a"> <i>this</i></label> 
<script> 
      $(document).ready(function() { 
       $('.i-checks').iCheck({ 
        checkboxClass: 'icheckbox_square-green', 
        radioClass: 'iradio_square-green' 
       }); 
      }); 
     </script> 

</div> 

       </td> 
      </tr> 
      </tbody> 
     </table> 
    </div> 
</div> 

這工作 - 但顯然它的一個可怕的想法,因爲jQuery是呈現每一行。所以我的問題是,這個代碼放在哪裏是最好的地方,所以它也可以工作?我應該把它放入控制器嗎?

道歉,如果我不清楚,我是Angular的新手。

非常感謝

+0

如果它與dom交互(在這種情況下,這是真的)它應該在一個指令。到目前爲止,我看到的唯一例外是創建服務來處理對話或警報。 – 2015-01-26 22:39:41

回答

1

公正的插件部分指令是真的很容易引發一起

angular.module('YourApp').directive('iChecks', function() {  
    return { 
     restrict: 'C', // for class in markup 
     link: function (scope, element, attrs) { 
      // element is jQuery object (see note below) 
      element.iCheck({ 
       checkboxClass: 'icheckbox_square-green', 
       radioClass: 'iradio_square-green' 
      }); 
     } 
    } 
}); 

將要求您之前在頁 angular.js包括的jquery.js 。當你這樣做時angular.element使用完整的jQuery庫。

接下來的步驟是添加行爲。請注意,任何修改示波器的事件都需要使用scope.$apply()來告訴角度運行摘要循環

+0

完美!那工作。謝謝 – 2015-02-25 16:58:55

0

正如您所說的正確,您所做的是一個可怕的想法。正如凱文在評論中已經說過的那樣,你應該創建一個指令。

我創建了一個小小的要點來啓用任意的JQuery插件。

https://gist.github.com/ameyms/4f3a5d6a8d329609d44e

PS:我沒有測試出來,所以不知道它的工作原理!

+1

只是一個建議,添加使用視圖的要點。 – charlietfl 2015-01-26 23:22:21

+1

增加使用。 https://gist.github.com/ameyms/4f3a5d6a8d329609d44e – Amey 2015-01-26 23:32:07