以下是我的指令代码:测试表指示问题
currentApp.directive('testList', ['$compile', function ($compile) {
return{
restrict: 'E',
template: '<table></table>',
replace: true,
compile: function (elem) {
return function ($scope, elem) {
var arr = ['Jay', 'John', 'David'];
angular.forEach(arr, function (val) {
elem.append('<tr><td>' + val + '</td></tr>');
});
$compile(elem)($scope);
}
}
}
}]);
,这是我的测试代码:
describe('Tabletest', function() {
var element, scope, elem;
var linkingFn;
beforeEach(module('angularlearnApp'));
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope;
elem = angular.element('<test-list></test-list> ');
linkingFn = $compile(elem);
element = linkingFn(scope);
}));
it('Testing table', function() {
console.info(element);
});
});
这是测试的输出:
Object{0: <table class="ng-scope">JayJohnDavid</table>, 1: , length: 2}
我我只是想知道为什么tr和td标签没有出现在这里,只有值出现。
没有真正回答您的具体问题,但你应该利用NG重复指令。例如... http://jsfiddle.net/HB7LU/1511/ –
你能为此创建一个jsfiddle吗? – dcodesmith
我然而,在它出现这样的测试与NG-重复的方法试过: