angular 动态遍历数组中的字典

问题描述:生成一个表格,表头信息存放在$scope.options.theads里面,数据格式为

[{
'rows': [{
'isedit': ' 0 ',
'title': 'pubtime ',
'iscombine': ' 0 ',
'colspan': 1,
'rowspan': 1,
'field': 'xxx '
}, {
'isedit': ' 0 ',
'title': 'transmit同比增长率',
'iscombine': ' 0 ',
'colspan': 1,
'rowspan': 1,
'field': 'xxx'
}]
},
{...},
{...},
{...}]

需求:需要修改表头,而页面遍历使用这样(对数组中的字典进行遍历,字典的value是一个数组)

<tr ng-repeat="(theadkeys,theadrows) in option.theads track by theadkeys">
    <th  ng-repeat="(theadkey,theadrow) in theadrows.rows track by theadkey" rowspan="{{ theadrow.rowspan }}" colspan="{{ theadrow.colspan }}">{{ theadrow.title }}</th>
</tr>

bug:用弹出框传值(angular.copy)修改了以后发现表头直接没有了,因为修改了options,字典(theadkey,theadrow) in theadrows.rows不会再自动遍历,表现的结果是表头直接没有了。


解决办法:

弹出框的返回值里对那段遍历代码用$compile,

modelInstance.result.then(function (data) {
    if(data.type=='save'){
        $scope.option.theads[theadsCnt-1]=data.data;//我这里的表头不止一个所以用数组,但无关紧要
        htmlStr='<thead>\n' +
            '        <tr ng-repeat="(theadkeys,theadrows) in option.theads track by theadkeys">\n' +
            '              <th  ng-repeat="(theadkey,theadrow) in theadrows.rows track by theadkey" rowspan="{{ theadrow.rowspan }}" colspan="{{ theadrow.colspan }}">{{ theadrow.title }}</th>\n' +
            '        </tr>\n' +
            '    </thead>';
        $("#demodiv").find('thead').remove();
        $("#demodiv").prepend($compile(htmlStr)($scope));//这里必须要用$compile()($scope)不然不起作用
    }
})



参考:https://blog.csdn.net/oucqsy/article/details/76178140        这篇里是用directive遍历,我这里用起来不合适

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值