AngularJS Typescript服务注入错误

时间:2018-12-22 01:31:27

标签: angularjs typescript

我正在尝试将AngularJS迁移到typesript。如果遇到使用静态$ inject定义注入的情况,我面临一个testController未定义的怪异问题。

控制器TS

class TestController {
    private customerName : string;
    private customerLink : string;
    private contactDetails : any;
    private userPosts : any;

    static $inject = ["TestService", "CustomerContactService"];
    constructor(private TestService: ITestService, private CustomerContactService: any) {
        console.log("inside constructor TestController");
    }

    init(customerName, customerLink) {
        if(customerName && customerLink) {
            this.customerName = TestConstant.HELLO + customerName;
            this.customerLink = customerLink;
        }
    }

    getPosts() {
        this.testService.getUserPosts().then((response : ng.IHttpPromiseCallbackArg<any>) =>{
            this.userPosts = JSON.stringify(response.data);
        });
    }

    getContactDetails() {
        this.customerContactService.getContacts("sales").then((response : ng.IHttpPromiseCallbackArg<any>) =>{
            this.contactDetails = JSON.stringify(response);
        });
    }
}

angular
    .module("testModule")
    .controller("testController", TestController);

服务TS

interface ITestService {
    getUserPosts() : ng.IPromise<ng.IHttpPromiseCallbackArg<any>>;
}

class TestService implements ITestService {

    static $inject = ['$http'];
    constructor(private $http : ng.IHttpService) {

    }

    getUserPosts() : ng.IPromise<ng.IHttpPromiseCallbackArg<any>> {
        return this.$http.get("https://jsonplaceholder.typicode.com/posts/1");
    }
}

angular
    .module("testModule")
    .service("testService", TestService);

但是,如果我在控制器

中使用以下内容
static AngularDependencies = ["TestService", "CustomerContactService"];
OR
static helloWorld = ["TestService", "CustomerContactService"];

一切正常。我究竟做错了什么?以及如何使用静态$ inject,因为这似乎是一个约定?

错误:[ng:areq] http://errors.angularjs.org/1.3.15/ng/areq?p0=testController&p1=not%20a%20function%2C%20got%20undefined

TIA

0 个答案:

没有答案