X Tutup
Skip to content

Commit 99e6500

Browse files
0x-r4bbitalexeagle
authored andcommitted
feat(upgrade): support bindToController with binding definitions
Since angular 1.4 we can also pass controller bindings directly to bindToController, making this syntax more convenient Closes #4784
1 parent 5c782d6 commit 99e6500

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

modules/angular2/src/upgrade/upgrade_ng1_adapter.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ export class UpgradeNg1ComponentAdapterBuilder {
7878
}
7979

8080
extractBindings() {
81-
var scope = this.directive.scope;
82-
if (typeof scope == 'object') {
83-
for (var name in scope) {
84-
if ((<any>scope).hasOwnProperty(name)) {
85-
var localName = scope[name];
81+
var btcIsObject = typeof this.directive.bindToController === 'object';
82+
if (btcIsObject && Object.keys(this.directive.scope).length) {
83+
throw new Error(
84+
`Binding definitions on scope and controller at the same time are not supported.`);
85+
}
86+
87+
var context = (btcIsObject) ? this.directive.bindToController : this.directive.scope;
88+
89+
if (typeof context == 'object') {
90+
for (var name in context) {
91+
if ((<any>context).hasOwnProperty(name)) {
92+
var localName = context[name];
8693
var type = localName.charAt(0);
8794
localName = localName.substr(1) || name;
8895
var outputName = 'output_' + name;
@@ -109,7 +116,7 @@ export class UpgradeNg1ComponentAdapterBuilder {
109116
this.propertyMap[outputName] = localName;
110117
break;
111118
default:
112-
var json = JSON.stringify(scope);
119+
var json = JSON.stringify(context);
113120
throw new Error(
114121
`Unexpected mapping '${type}' in '${json}' in '${this.name}' directive.`);
115122
}

modules/angular2/test/upgrade/upgrade_spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,35 @@ export function main() {
417417
});
418418
}));
419419

420+
it('should support bindToController with bindings', inject([AsyncTestCompleter], (async) => {
421+
var adapter = new UpgradeAdapter();
422+
var ng1Module = angular.module('ng1', []);
423+
424+
var ng1 = function() {
425+
return {
426+
scope: {},
427+
bindToController: {title: '@'},
428+
template: '{{ctl.title}}',
429+
controllerAs: 'ctl',
430+
controller: Class({constructor: function() {}})
431+
};
432+
};
433+
ng1Module.directive('ng1', ng1);
434+
var Ng2 = Component({
435+
selector: 'ng2',
436+
template: '<ng1 title="WORKS"></ng1>',
437+
directives: [adapter.upgradeNg1Component('ng1')]
438+
}).Class({constructor: function() {}});
439+
ng1Module.directive('ng2', adapter.downgradeNg2Component(Ng2));
440+
var element = html(`<div><ng2></ng2></div>`);
441+
adapter.bootstrap(element, ['ng1'])
442+
.ready((ref) => {
443+
expect(multiTrim(document.body.textContent)).toEqual('WORKS');
444+
ref.dispose();
445+
async.done();
446+
});
447+
}));
448+
420449
it('should support single require in linking fn', inject([AsyncTestCompleter], (async) => {
421450
var adapter = new UpgradeAdapter();
422451
var ng1Module = angular.module('ng1', []);

0 commit comments

Comments
 (0)
X Tutup