【发布时间】:2021-04-07 20:28:38
【问题描述】:
目前试图弄清楚为什么会发生这种情况,但如果我在 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,以便我可以正确分配我的其他属性,但目前它不起作用:/
我也尝试删除?,所以从&? 到&,但现在可能是NG1 提供angular.noop ..
关于如何在不重写内容的情况下完成这项工作的任何想法?
非常感谢您提供的任何帮助!
注意:我们希望将一部分代码库保留在 NG1 中,因为我们现在无法使用现有代码库转换所有代码库。
【问题讨论】:
标签: angularjs angular angular-upgrade