以下是我的指令代碼:測試表指示問題
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-重複的方法試過: