Javascript 如何使用angularjs中的ng repeat动态设置css中表格列的宽度

Javascript 如何使用angularjs中的ng repeat动态设置css中表格列的宽度,javascript,jquery,html,css,angularjs,Javascript,Jquery,Html,Css,Angularjs,您好,我正在处理一个UI,它有一个表,其中的列应该根据数据中提供的百分比调整其宽度。我使用ng repeat获取表列中的数据,但不知道如何为接收到的每个数据设置不同的宽度。这是我的js代码 var-App=angular.module(“myApp”,[]); 应用控制器(“主控制器”,功能($scope){ $scope.info=[ {名称:'aob', 人数:20%}, {名称:'cc', 人数:15%}, {名称:'mb', 人数:20%}, {名称:'ts', 数字:6%}, {名称

您好,我正在处理一个UI,它有一个表,其中的列应该根据数据中提供的百分比调整其宽度。我使用ng repeat获取表列中的数据,但不知道如何为接收到的每个数据设置不同的宽度。这是我的js代码

var-App=angular.module(“myApp”,[]);
应用控制器(“主控制器”,功能($scope){
$scope.info=[
{名称:'aob',
人数:20%},
{名称:'cc',
人数:15%},
{名称:'mb',
人数:20%},
{名称:'ts',
数字:6%},
{名称:'ws',
数字:8%},
{name:'t',
数字:8%},
{名称:'aob',
数量:2%},
{名称:'pb',
数量:25%}
]
}

);我最接近的地方是:

    <table ng-repeat="item in items">
    <tr>
        <td width="{{item.value}}"> {{item.name}} </td>
        <td width="{{item.value}}"> {{item.value}} </td>
    </tr>
</table>
仅用于测试目的(没有与您相同的数据)

更新答案:我将
ng style=”“
替换为
width=“


这对我来说是可行的,但正如您所说的,您不能在
级别上重复,但我不知道其他方法如何执行。

一个想法可以是,根据您的喜好,为列或行创建一个指令,用指令中生成的自定义行或列替换该行或列,该指令根据您的标准具有自定义宽度。该示例引用了一个td,它可以根据需要进行转换。希望这有帮助

angular.module('appname')
    .directive('tddirective', ['$compile',
        function ($compile) {
            return {
                scope: {
                    item: '='
                },
                replace: true,
                link: function (scope, element, attrs) {
                    var html = "";

                    function addElement(html) {
                        var toInsert = angular.element(html);
                        element.append(toInsert);
                        $compile(toInsert)(scope);
                    }

                    //Here you make the validation or what do you want for stablish the correct width, even you can compare with anothers columns and  format the width in any case
                    html = '\
                           <td style="width: " + item.width +";">item.name</td>';
                    addElement(html);
                 }
            }
        }])
angular.module('appname'))
.directive('tddirective',['$compile',
函数($compile){
返回{
范围:{
项目:'='
},
替换:正确,
链接:函数(范围、元素、属性){
var html=“”;
函数附加元素(html){
var toInsert=angular.element(html);
元素。追加(toInsert);
$compile(toInsert)(范围);
}
//在这里,您可以进行验证或确定正确的宽度,甚至可以在任何情况下与其他列进行比较并格式化宽度
html='1〕\
项目名称';
附加元素(html);
}
}
}])

为什么要为占位符添加额外的列名称占位符仅供参考,以查看宽度是否正确它实际上不起作用如果我给ng repeat in table标记,表格每次都在重复,我希望列数据重复,我会查看它并更新我的答案,如果我找到了任何东西,很多,但我能够做到非常感谢你的帮助
angular.module('appname')
    .directive('tddirective', ['$compile',
        function ($compile) {
            return {
                scope: {
                    item: '='
                },
                replace: true,
                link: function (scope, element, attrs) {
                    var html = "";

                    function addElement(html) {
                        var toInsert = angular.element(html);
                        element.append(toInsert);
                        $compile(toInsert)(scope);
                    }

                    //Here you make the validation or what do you want for stablish the correct width, even you can compare with anothers columns and  format the width in any case
                    html = '\
                           <td style="width: " + item.width +";">item.name</td>';
                    addElement(html);
                 }
            }
        }])