2014-09-05 60 views
1

我在Jquery中有一個條形圖,我想要在角度上重新創建。該圖只是程式化的div這就是寬度設置爲父的比例如下圖所示在angular.js中設置div的寬度作爲父級的百分比

$('.item-skills').each(function(){ 
    newWidth = $(this).parent().width() * $(this).data('percent'); 
    $(this).width(newWidth); 
}); 

我希望把這個稱爲bar-graph指令使用像

<div bar-graph="0.85"></div> 

從而會產生DIV這就是寬度是父母的85%

編輯:我發現了一個解決方案在這裏Angularjs adjust width based on parent element width

+0

但'酒吧graph'不會是一個有效的屬性我的解決方案,以creating directives鏈接 – Pete 2014-09-05 13:55:51

回答

0

這裏是下面是否有人需要幫助

(function(angular) { 

     var 
     definitions; 

     definitions = [ 
     niBargraph 
     ]; 

     angular.module('ni.Bargraph').directive('niBargraph', definitions); 

     function niBargraph() { 

     return { 
      scope: { 
      percent: '@niBargraph' 
      }, 
      link: setSize 
     }; 

     function setSize(scope, elm, attrs, ctrl) { 
      elm.css({ 
       width: elm.parent()[0].offsetWidth * parseFloat(scope.percent) 
      }); 
     } 
     } 
    })(angular); 
相關問題