创建使用打字稿AngularJs控制器 [英] Create AngularJs controller using TypeScript

查看:105
本文介绍了创建使用打字稿AngularJs控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有JS code与角控制器,它开始是这样的:

I have JS code with Angular controller which starts like this:

angular.module('myApp.controllers', [])
    .controller('BookmarksController', function($scope, bookmarkService, crawlerService) {

我想用打字稿重写一遍。这里是我的code:

I want to rewrite it using TypeScript. Here is my code:

class Some {

    constructor($scope, bookmarkService, crawlerService) {} 
}

angular
    .module('myApp.controllers', [])
    .controller('BookmarksController', Some($scope, bookmarkService, crawlerService));

它不工作:

找不到名称$范围,bookmarkService,crawlerService

Can not find name $scope, bookmarkService, crawlerService

我怎样才能解决这个问题?

How can I fix it ?

推荐答案

最好的办法的(当然,解决办法准备缩小)的将是:

class Some {
    // instruction for IoC
    static $inject = ["$scope", "bookmarkService", "crawlerService"];

    constructor($scope, bookmarkService, crawlerService) {} 
}

angular
    .module('myApp.controllers', [])
    .controller('BookmarksController', Some);

如果我们使用的 静态$注射= [] 数组的IoC的名字,这将缩小甚至工作后。然后,我们刚刚注册控制器类

Where we are using the static $inject = [] array for IoC names, which will work even after minification. Then we just register the controller class

.controller('BookmarksController', Some);

和后角国际奥委会将正确地找出应注射作为构造PARAMS

and later angular IoC will properly find out what should be injected as constructor params

在情况下,我们会使用一些模块/命名空间

In case we would use some modules/namespaces

module My.Namespace
{
    class Some {

我们可以使用:

.controller('BookmarksController', My.Namespace.Some);

这篇关于创建使用打字稿AngularJs控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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