根据Todd Motto's styleguide, Controllers chapter:角:依赖注入与原型继承
继承:扩展的控制器类时使用原型继承
我尝试实施它在我的控制器:
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
中需要用于与服务器端通信的3210。是否有可能将其注入CommunicationService
注入TagModalController
?然后我的代码看起来就像这样:
function BaseController(CommunicationService){
'use strict';
this._path = '';
this._restService = CommunicationService;
}
和方法this.setRestService(CommunicationService);
将不再需要。有没有什么方法可以在AngularJS中为控制器实现Angular DI的原型继承?预先感谢您的每一个答案。