Create an angular service with a typescript rest Client

◇◆丶佛笑我妖孽 提交于 2019-12-11 10:33:37

问题


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;
}

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


回答1:


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) {
}


来源:https://stackoverflow.com/questions/33831325/create-an-angular-service-with-a-typescript-rest-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!