依赖注入AngularJS控制器,它可以更简单吗? [英] Dependency injection into AngularJS controller, can it be more simple?
本文介绍了依赖注入AngularJS控制器,它可以更简单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我app.ts,我有以下的code现在:
In my app.ts , I have the following code right now:
.controller('aboutHelper', ['$scope', 'sharedService', function ($scope, sharedService) {
var aboutContr = new Controllers.AboutController($scope, sharedService);
}])
的 Controllers.AboutController 的是我的类,它包含实际的逻辑,我controller.This方式我有三次输入每个依赖。这看起来不是很好。 有没有更好的方式来做事?
Controllers.AboutController is my class which contains the actual logic for my controller.This way I have to type every dependency three times. This doesn't look very good. Is there a better way to do things?
感谢
推荐答案
在你的 AboutController
您可以定义静态属性 $注
(在这里阅读更多)
In your AboutController
you can define static property $inject
(read more here)
该控制器是这样的话:
module Controllers {
export class AboutController {
static $inject = ["$scope", "sharedService"];
constructor($scope: IAboutScope, sharedService: ISharedService) {
}
}
}
然后该控制器将注册的角度是这样的:
Then this controller would be registered for angular like this:
app.controller('aboutCtrl', Controllers.AboutController)
这篇关于依赖注入AngularJS控制器,它可以更简单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文