小编cha*_*obo的帖子

使用TypeScript返回AngularJS $ q承诺

我有一个服务,包含$ http我的函数返回一个延迟对象.

我的界面:

export interface MyServiceScope {
    get: ng.IPromise<{}>;
}
Run Code Online (Sandbox Code Playgroud)

我的课:

export class MyService implements MyServiceScope {

    static $inject = ['$http', '$log'];

    constructor(private $http: ng.IHttpService,
                private $log: ng.ILogService,
                private $q: ng.IQService) {
        this.$http = $http;
        this.$log = $log;
        this.$q = $q;
    }

    get(): ng.IPromise<{}> {
        var self = this;
        var deferred = this.$q.defer();

        this.$http.get('http://localhost:8000/tags').then(
            function(response) {
                deferred.resolve(response.data);
            },
            function(errors) {
                self.$log.debug(errors);
                deferred.reject(errors.data);
            }
        );

        return deferred.promise;
    }
}
Run Code Online (Sandbox Code Playgroud)

编译失败,出现以下错误:

myservice.ts(10,18): error TS2420: Class 'MyService' incorrectly implements interface 'MyServiceScope'.
    Types of …
Run Code Online (Sandbox Code Playgroud)

deferred angularjs typescript angular-promise

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

angular-promise ×1

angularjs ×1

deferred ×1

typescript ×1