Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 7e86eac

Browse files
committed
fix($compile): relax the restriction that directives can not add siblings
Relax the restriction that directives can not add siblings
1 parent 15c1fe3 commit 7e86eac

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

src/ng/compiler.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ function $CompileProvider($provide) {
350350
var linkingFns = [],
351351
directiveLinkingFn, childLinkingFn, directives, attrs, linkingFnFound;
352352

353-
for(var i = 0, ii = nodeList.length; i < ii; i++) {
353+
for(var i = 0; i < nodeList.length; i++) {
354354
attrs = new Attributes();
355355

356356
// we must always refer to nodeList[i] since the nodes can be replaced underneath us.
@@ -374,10 +374,6 @@ function $CompileProvider($provide) {
374374
return linkingFnFound ? linkingFn : null;
375375

376376
/* nodesetLinkingFn */ function linkingFn(scope, nodeList, rootElement, boundTranscludeFn) {
377-
if (linkingFns.length != nodeList.length * 2) {
378-
throw Error('Template changed structure!');
379-
}
380-
381377
var childLinkingFn, directiveLinkingFn, node, childScope, childTransclusionFn;
382378

383379
for(var i=0, n=0, ii=linkingFns.length; i<ii; n++) {

test/ng/compilerSpec.js

+33-10
Original file line numberDiff line numberDiff line change
@@ -242,16 +242,39 @@ describe('$compile', function() {
242242
});
243243

244244

245-
it('should prevent changing of structure', inject(
246-
function($compile, $rootScope){
247-
element = jqLite("<div><div log></div></div>");
248-
var linkFn = $compile(element);
249-
element.append("<div></div>");
250-
expect(function() {
251-
linkFn($rootScope);
252-
}).toThrow('Template changed structure!');
253-
}
254-
));
245+
it('should allow changing the template structure after the current node', function() {
246+
module(function($compileProvider){
247+
$compileProvider.directive('after', valueFn({
248+
compile: function(element) {
249+
element.after('<span log>B</span>');
250+
}
251+
}));
252+
});
253+
inject(function($compile, $rootScope, log){
254+
element = jqLite("<div><div after>A</div></div>");
255+
$compile(element)($rootScope);
256+
expect(element.text()).toBe('AB');
257+
expect(log).toEqual('LOG');
258+
});
259+
});
260+
261+
262+
it('should allow changing the template structure after the current node inside ngRepeat', function() {
263+
module(function($compileProvider){
264+
$compileProvider.directive('after', valueFn({
265+
compile: function(element) {
266+
element.after('<span log>B</span>');
267+
}
268+
}));
269+
});
270+
inject(function($compile, $rootScope, log){
271+
element = jqLite('<div><div ng-repeat="i in [1,2]"><div after>A</div></div></div>');
272+
$compile(element)($rootScope);
273+
$rootScope.$digest();
274+
expect(element.text()).toBe('ABAB');
275+
expect(log).toEqual('LOG; LOG');
276+
});
277+
});
255278
});
256279

257280
describe('compiler control', function() {

0 commit comments

Comments
 (0)