Angular:具有原型继承的依赖注入 [英] Angular: Dependency Injection with prototypal inheritance

本文介绍了Angular:具有原型继承的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Todd Motto 的风格指南,控制器章节:

继承:扩展控制器类时使用原型继承

Inheritance: Use prototypal inheritance when extending controller classes

我尝试在我的控制器中实现它:

I try to implement it in my controllers:

function BaseController(){
    'use strict';

    this._path = '';
    this._restService = undefined;
}

/**
 * Boring JSDocs
 */
BaseController.prototype.getById = function(id) {
    return this._restService.getById(this._path, id);
};

TagModalController.prototype = Object.create(BaseController.prototype);

/**
 * Even more boring JSDocs
 */
function TagModalController(CommunicationService, $modalInstance, mode, 
    id, CommandService) {
    'use strict';

    // boring assertions

    this.setPath('tags');
    this.setRestService(CommunicationService);

但是,如您所见,我必须始终设置 BaseController 中所需的 restService,以便与服务器端进行通信.是否有可能像将 CommunicationService 注入 TagModalController 一样注入它?然后我的代码将如下所示:

But, as you can see, I have to always set restService which is needed in BaseController for communication with server side. Is any possibility to inject it like CommunicationService is injected into TagModalController? Then my code will look like this:

function BaseController(CommunicationService){
    'use strict';

    this._path = '';
    this._restService = CommunicationService;
}

并且方法 this.setRestService(CommunicationService); 将不再需要.有什么方法可以通过 AngularJS 中的控制器的原型继承来实现 Angular DI?预先感谢您的每一个回答.

And method this.setRestService(CommunicationService); won't be needed anymore. Is there any way to achieve Angular DI with prototypal inheritance for controllers in AngularJS? Thank you in advance for every answer.

推荐答案

这可以被认为是一种必要的邪恶.即使使用 ES2015 类,父类构造函数的参数 必须是使用 super 明确指定.

This can be considered a necessary evil. Even with ES2015 classes the arguments for parent class constructor have to be specified explicitly with super.

你可能想借用类用于继承的模式并做

You may want to borrow the pattern that classes use for inheritance and do

angular.bind(this, BaseController)(CommunicationService, ...);

将变量传递给 BaseController 构造函数,尤其是当存在多个依赖项时.

to pass variables to BaseController constructor, especially if there is more than one dependency that should be passed there.

BaseController 不是由 $controller 实例化,因此不会从 Angular DI 中受益,this 是唯一将 BaseController 与子项关联的东西.为了让它能够访问 TagModalController 注入的依赖项,它们必须被分配为 this 属性,从而暴露给作用域(Angular 控制器在设计时没有考虑到 this,而 controllerAs 只是弥补 $scope 缺点的语法糖).这不是一件好事.

BaseController isn't instantiated by $controller and thus doesn't benefit from Angular DI, this is the only thing that associates BaseController with children. In order to give it an access to TagModalController injected dependencies, they have to be assigned as this properties and thus exposed to scope (Angular controllers weren't designed with this in mind, and controllerAs is just syntactic sugar to remedy $scope drawbacks). Which is not a very good thing.

这篇关于Angular:具有原型继承的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆