X Tutup
Skip to content

Commit 19c1bd7

Browse files
committed
feat(ngUpgrade): transclude content from ng2->ng1
Closes #4640
1 parent 84c3124 commit 19c1bd7

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

modules/upgrade/src/angular.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ declare namespace angular {
77
run(a: any);
88
}
99
interface ICompileService {
10-
(element: Element): (IScope) => void;
10+
(element: Element, transclude?: Function): ILinkFn;
11+
}
12+
interface ILinkFn {
13+
(scope: IScope, cloneAttachFn?: Function, options?: ILinkFnOptions): void
14+
}
15+
interface ILinkFnOptions {
16+
parentBoundTranscludeFn?: Function, transcludeControllers?: {[key: string]: any},
17+
futureParentElement?: Node
1118
}
1219
interface IRootScopeService {
1320
$new(): IScope;

modules/upgrade/src/ng1_facade.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,16 @@ class Ng1ComponentFacade implements OnChanges, DoCheck {
112112
private inputs: string[], private outputs: string[], private propOuts: string[],
113113
private checkProperties: string[], private propertyMap: {[key: string]: string}) {
114114
var chailTail = scope.$$childTail; // remember where the next scope is inserted
115-
compile(elementRef.nativeElement)(scope);
116-
115+
var element: Element = elementRef.nativeElement;
116+
var childNodes: Node[] = [];
117+
var childNode;
118+
while (childNode = element.firstChild) {
119+
element.removeChild(childNode);
120+
childNodes.push(childNode);
121+
}
122+
element.appendChild(element.ownerDocument.createElement('ng-transclude'));
123+
compile(element)(scope, null,
124+
{parentBoundTranscludeFn: (scope, cloneAttach) => cloneAttach(childNodes)});
117125
// If we are first scope take it, otherwise take the next one in list.
118126
this.componentScope = chailTail ? chailTail.$$nextSibling : scope.$$childHead;
119127

modules/upgrade/test/integration_spec.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,26 @@ export function main() {
3434
});
3535
}));
3636

37-
it('should instantiate ng1 in ng2 template', inject([AsyncTestCompleter], (async) => {
38-
var upgradeModule: UpgradeModule = createUpgradeModule();
37+
it('should instantiate ng1 in ng2 template and project content',
38+
inject([AsyncTestCompleter], (async) => {
39+
var upgrMod: UpgradeModule = createUpgradeModule();
3940

4041
var Ng2 = Component({selector: 'ng2-1'})
4142
.View({
42-
template: `{{ 'ng2(' }}<ng1></ng1>{{ ')' }}`,
43-
directives: [upgradeModule.exportAsNg2Component('ng1')]
43+
template: `{{ 'ng2(' }}<ng1>{{'transclude'}}</ng1>{{ ')' }}`,
44+
directives: [upgrMod.exportAsNg2Component('ng1')]
4445
})
4546
.Class({constructor: function() {}});
4647

47-
upgradeModule.ng1Module.directive('ng1',
48-
() => { return {template: 'ng1 {{ "WORKS" }}!'}; });
49-
upgradeModule.importNg2Component(Ng2);
48+
upgrMod.ng1Module.directive('ng1', () => {
49+
return {transclude: true, template: '{{ "ng1" }}(<ng-transclude></ng-transclude>)'};
50+
});
51+
upgrMod.importNg2Component(Ng2);
5052

5153
var element = html("<div>{{'ng1('}}<ng2-1></ng2-1>{{')'}}</div>");
5254

53-
upgradeModule.bootstrap(element).ready(() => {
54-
expect(document.body.textContent).toEqual("ng1(ng2(ng1 WORKS!))");
55+
upgrMod.bootstrap(element).ready(() => {
56+
expect(document.body.textContent).toEqual("ng1(ng2(ng1(transclude)))");
5557
async.done();
5658
});
5759
}));

0 commit comments

Comments
 (0)
X Tutup