angularjs动态设置绑定或模型

时间:2013-11-03 19:44:47

标签: javascript angularjs

我正在尝试动态填充表格中的列:http://jsfiddle.net/REjUv/1/

$ scope有一个列定义数组,每个列定义都有一个属性名称和行的项目数组

我正在尝试将td元素的内容设置为具有相关列中名称的项目的属性

<div ng-app ng-controller='ListController'>
    <table>
        <tr>
            <th ng-repeat='column in columns'>{{column.text}}</th>
        </tr>
        <tr ng-repeat='item in items'>
            <td ng-repeat='column in columns'>
                <!--how do i set the ng-bind to item.[column.name]-->
                <label type='text' ng-bind='item.name' />
            </td>
        </tr>
    </table>
</div>


function ListController($scope){
    $scope.columns = [
        { name: 'id', text: 'Id' },
        { name: 'code', text: 'Code' },
        { name: 'name', text: 'Name' }
    ];

    $scope.items = [
        { id:1, code: 'A', name: 'AAA' },
        { id:2, code: 'B', name: 'BBB' },
        { id:3, code: 'C', name: 'CCC' },
        { id:4, code: 'D', name: 'DDD' }
    ];
}

1 个答案:

答案 0 :(得分:0)

此?

<td ng-repeat='column in columns'>{{item[column.name]}</td>

FIDDLE