我試圖讓ng-repeat在Angular指令測試中工作,但是當$ scope變量更新並且$ digest()叫做。Angular ng-repeat在茉莉花測試中不起作用
'use strict';
describe('Asset tests', function() {
var $compile,
$rootScope,
$scope,
$timeout,
templateHtml = '<span ng-repeat="asset in assets">{{asset.id}}</span>',
view;
beforeEach(function() {
module('my.module');
inject(function(_$compile_, _$rootScope_, _$timeout_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
$scope = $rootScope.$new();
$scope.assets = [];
$scope.selectedItems = [];
$scope.deleteAsset = function() {
console.log('yay');
};
view = angular.element(templateHtml);
$compile(view)($scope);
$scope.$apply();
});
});
describe('Testing some stuff', function() {
it('should test some stuff', function() {
for(var i = 0; i < 3; i ++) {
$scope.assets.push({id:i, name: 'Rad ' + i});
};
$scope.$digest();
console.log(view);
console.log(jQuery('span').length);
});
});
});
日誌報表輸出:
LOG: {0: <span ng-repeat="assets in assets">{{asset.id}}</span>, length: 1}
LOG: 56
感謝您指出這些事情。仍然得到相同的結果。請參閱編輯原始代碼。 – binarygiant 2014-09-22 15:21:57