目前试图弄清楚为什么会发生这种情况,但如果我&?在 NG1 组件上有一个可选的函数绑定 (),那么 NG2 将在其上放置一个 EventEmitter:/
这不是我所期望的,并且会破坏我们通常在null/时所做的一些代码undefined。
这是我们所拥有的示例:
class MyController {
public onInit: () => string;
public onAction: () => void;
public resultFromNg2: string;
public onActionHasValue: boolean;
public typeOfOnAction: string;
public $onInit() {
this.resultFromNg2 = this.onInit();
this.onActionHasValue = this.onAction != null;
this.typeOfOnAction = typeof this.onAction;
}
}
export const ng1DemoComponent = {
selector: "ng1-demo",
template: `
<div>Hello, {{ $ctrl.username }}!</div>
<div>resultFromNg2: {{ $ctrl.resultFromNg2 }}</div>
<div>onActionHasValue: {{ $ctrl.onActionHasValue }} - typeof: {{ $ctrl.typeOfOnAction }}</div>
`,
bindings: {
username: "<",
onInit: "&?",
onAction: "&?"
},
controller: MyController
};
@Directive({
selector: ng1DemoComponent.selector
})
export class Ng1DemoComponentFacade extends UpgradeComponent {
@Input() username: string;
@Input() onInit: () => string;
@Input() onAction: () => void;
constructor(elementRef: ElementRef, injector: Injector) {
super(ng1DemoComponent.selector, elementRef, injector);
}
}
可测试环境:https ://stackblitz.com/edit/angular-hybrid-upgrade-dpjv4p?file=ng1%2Fng1-demo.component.ts
在这段代码中,我希望onAction设置为undefined以便我可以正确分配我的其他属性,但目前它不起作用:/
我也尝试删除?, 所以 from &?to &,但现在可能是 NG1 给出了angular.noop..
关于如何在不重写内容的情况下完成这项工作的任何想法?
非常感谢您提供的任何帮助!
注意:我们希望在 NG1 中保留一部分代码库,因为我们现在无法使用我们拥有的代码库来转换所有代码库。