简体   繁体   English

将div的宽度设置为angular.js中父级的百分比

[英]Set a width of a div as a percentage of parent in angular.js

I have a bar graph in Jquery that I want to recreate in angular. 我在Jquery中有一个条形图,想要以角度重新创建。 The graph is just stylized divs thats width are set to a percentage of the parent as shown below 该图只是样式化的div,其宽度设置为父级的百分比,如下所示

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

I want to put this in a directive called bar-graph to use like 我想把它放在一个叫做bar-graph的指令中,像

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

Which would produce a div thats width is 85% of the parents 这将产生一个div宽度是父母的85%

Edit: I found a solution here Angularjs adjust width based on parent element width 编辑:我在这里找到了解决方案Angularjs根据父元素的宽度调整宽度

What you are after is a directive. 您所追求的是指令。 like this question on stackoverflow Here is a link to creating directives from AngularJS 像这个关于stackoverflow的问题一样这是从AngularJS 创建指令的链接

Here is my solution below if anyone else needs help 如果有人需要帮助,这是我的解决方案

    (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);

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

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