显然 Angular 2.0.0 需要将 NgModule 传递给 UpgradeAdapter 构造函数。我从@angular/update 中挖掘出以下有效的代码片段:
var adapter = new UpgradeAdapter(forwardRef(() => MyNg2Module));
var module = angular.module('myExample', []);
module.directive('greet', function () {
return {
scope: {salutation: '=', name: '='},
template: '{{salutation}} {{name}}! - <span ng-transclude></span>'
};
});
module.directive('ng2', adapter.downgradeNg2Component(Ng2));
@Component({
selector: 'ng2',
template: 'ng2 template: <greet salutation="Hello" [name]="world">text</greet>'
})
class Ng2 {
}
@NgModule({
declarations: [Ng2, adapter.upgradeNg1Component('greet')],
imports: [BrowserModule]
})
class MyNg2Module {
}
document.body.innerHTML = '<ng2></ng2>';
adapter.bootstrap(document.body, ['myExample']).ready(function () {
expect(document.body.textContent).toEqual("ng2 template: Hello world! - text");
});