依赖注入AngularJS控制器,它可以更简单吗?它可以、控制器、简单、AngularJS

由网友(沒了感覺了,還有什麽可說)分享简介:在我app.ts,我有以下的code现在:In my app.ts , I have the following code right now:.controller('aboutHelper', ['$scope', 'sharedService', function ($scope, sharedService...

在我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)
阅读全文

相关推荐

最新文章