创建带有打字稿REST客户端的角度服务 [英] Create an angular service with a typescript rest Client

查看:118
本文介绍了创建带有打字稿REST客户端的角度服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用与打字稿角1.4.7
我有一个自动生成的客户端与扬鞭的打字稿调用这样的REST服务:

I am using Angular 1.4.7 with typescript I have an autogenerated Client with Swagger in typescript to call a rest service like this:

module API.Client {
'use strict';

export class DefaultApi {
    ...

我试图做使用它:

I am trying to use it by doing:

import TDRService = API.Client.DefaultApi;

export class ListaTrtController {
    private tdrservice: TDRService;

/* @ngInject */
constructor(service: TDRService) {
     this.tdrservice = service;
}

但我收到错误未知提供商:&的ServiceProvider LT; - 服务< - ListaTrtController。
我怎样才能解决这个问题?

But I receive the error "Unknown provider: serviceProvider <- service <- ListaTrtController". How can I fix this problem?

推荐答案

在角度与您将在注射使用的名称注册为您服务。

Register your service in angular with the name that you will use in the injections.

module API.Client {
'use strict';

export class DefaultApi {
    ...

angular.module('yourmodule').service('TDRService', DefaultApi);

角将使用的变量名称(未的类型),所以它必须是相同的名称,因为它是在角登记。而且,您可以使用私人在构造函数,而不是像你这样做了分配。

Angular will use the variable name (Not the type), so it has to be the same name as it was registered in angular. And, you can use private in the constructor instead of doing the assignment like you did.

import TDRService = API.Client.TDRService;

export class ListaTrtController {

/* @ngInject */
constructor(private TDRService: TDRService) {
}

这篇关于创建带有打字稿REST客户端的角度服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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