简体   繁体   English

迁移 AngularJS 到 Angular:如何迁移获得 controller 参考的服务

[英]Migration AngularJS to Angular: How to migrate service which gets a controller reference

I am in the process of migrating an AngularJS-Application to Angular, component by component with the help of ngHybrid.我正在将 AngularJS 应用程序迁移到 Angular,在 ngHybrid 的帮助下逐个组件。 So far it works, but now I stumbled upon an weird AngularJS-Service with a function which gets passed an AngularJS-Component-Controller as function parameter:到目前为止它可以工作,但现在我偶然发现了一个奇怪的 AngularJS-Service,它带有一个 function,它作为 function 参数传递给 AngularJS-Component-Controller:

// Service
getDocumentList(requestData, ctrl) {
    // edits stuff on ctrl, like adding variables and modifying values
}

// calling component
var ctrl = this;
documentService.getDocumentList(requestData, ctrl);

I know I know, I for myself never would have created a service method which modifies a controller via passed reference, its horrible practice, but I am just migrating it and before refactoring it I'd rather make it work with as little effort as possible.我知道我知道,我自己永远不会创建一个通过传递引用修改 controller 的服务方法,这是一种可怕的做法,但我只是在迁移它,在重构它之前,我宁愿让它尽可能少地工作. I'm just not sure if it is possible with Angular to pass a reference, cause I don't have controllers anymore but classes... Is there a way to make it work it can I only refactor it so that the service returns something which the calling component works with (as it should be...)?我只是不确定 Angular 是否可以传递引用,因为我不再有控制器但类......有没有办法让它工作我只能重构它以便服务返回一些东西调用组件与哪个一起使用(应该是......)?

in Angular component class usually fulfills controller functions.在 Angular 组件中 class 通常实现 controller 功能。 so the same code - passing this should work.所以相同的代码 - 传递this应该可以工作。 but there can be issues with change detection, especially if you are using onPush strategy但是变更检测可能会出现问题,尤其是在您使用 onPush 策略时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM