2014-09-05 62 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); 
相关问题