用类构造angularjs指令

时间:2018-03-17 17:00:06

标签: angularjs typescript

这就是我目前的做法:

class AService {
    $http: any;
    $state: any;
    static $inject = ['$http', '$state'];
    constructor($http, $state) {
        this.$http = $http;
        this.$state = $state;
    };

它有很多样板代码来满足打字稿。有没有更短的方式?

1 个答案:

答案 0 :(得分:1)

您可以指定参数应直接存储在构造函数中。

class AService {
    static $inject = ['$http', '$state'];
    constructor(private $http, private $state) {
    }
}

当您使用angularjs模块注册类型并从类中删除$inject时,您也可以指定注入参数数组($inject)。