如何在组件中显示ng-repeat
作为表?为什么我的项目组件不显示数据为表行?
这是一个plunker,也是代码:
index.html
<body ng-controller="MainCtrl as vm">
<table border="1">
<tbody>
<item data="name" ng-repeat="name in vm.names"></item>
</tbody>
</table>
</body>
item.html
<tr>
<td>{{$ctrl.data.first}}</td>
<td>{{$ctrl.data.family}}</td>
</tr>
app.js
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
var vm = this;
vm.names = [{family: "dela cruz", first: "juan"}, {family: "tamad", first: "juan"}]
});
angular.module('plunker').component('item', {
templateUrl: 'item.html',
bindings: {
data: '='
}
});
请记住,该元素保留在表中,并且倾向于打破标准的HTML表 -> tbody-> tr-> TD结构。
对于这种特殊情况,我会执行以下操作:
angular
.module('sampleApp', [])
.controller('MainCtrl', function() {
var vm = this;
vm.names = [{family: "dela cruz", first: "juan"}, {family: "tamad", first: "juan"}];
})
.component('nameTable', {
template: [
'<table border="1">',
'<tbody>',
'<tr ng-repeat="name in $ctrl.data">',
'<td>{{name.first}}</td>',
'<td>{{name.family}}</td>',
'</tr>',
'</tbody>',
'</table>'
].join(''),
bindings: {
data: '<'
}
});
<body ng-app="sampleApp" ng-controller="MainCtrl as vm">
<name-table data="vm.names" ></name-table>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.6/angular.min.js"></script>
</body>