0

I'm trying to figure out how to use TypeScript in an existing Angular 1.5 app. I can't figure out how to inject custom services. Angular services and 3rd party services work fine. The services I need to inject are still vanilla JS services. In the example below I need to inject reportService.

The error I get is getReportById is not a function.

class ReportController implements IReportController {

    static $inject = ['$stateParams', 'reportService'];


    constructor(private $stateParams: angular.ui.IStateParamsService, private reportService) {

        this.getReport($stateParams.id);
    }

    getReport(id: number): object {
        reportService.getReportById(id)
            .then(function(res) {
                console.log(res)
            });
    }

}

Here is the service.

angular.module('reportsServiceModule', [])
    .factory('reportsService', [
        '$http',
        reportsService
    ]);

function reportsService(
        $http
) {

    'use strict';

    var service = {};

    service.getReportById = function(reportID) {
        return 'A promise';
    };

    return service;
}
4
  • And what is reportService? Commented Mar 13, 2017 at 23:20
  • reportService is a regular angular 1 service (could be any service) written in JavaScript (no TypeScript). Commented Mar 14, 2017 at 1:28
  • Please, provide the code for it. It matters. For any service you can use any type, but you won't benefit from TS this way. Do you have particular problems with the code above? Commented Mar 14, 2017 at 1:42
  • Adding any doesn't fix the issue, I've added the service structure. Commented Mar 14, 2017 at 18:06

2 Answers 2

1

The only real difference between JS and TS development in AngularJS is that the units can be typed. This doesn't affect anything but type safety.

Here it would be

interface IReportService {
 getReportById: (someType) => string;
}

...
constructor(
  private $stateParams: angular.ui.IStateParamsService,
  private reportService: IReportService
) {
...

There's no problem with dependency injection. If you have DI problem in Angular, you get an error from injector.

The real problem here is that strict mode isn't used. With use strict statement or alwaysStrict TS option the error would show what's really wrong - reportService variable is not defined in getReport method.

Instead, it should be

getReport(id: number): object {
    this.reportService.getReportById(id)
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I figured this out later in the day and this was the issue. I also had an order issue with my constructor, I had added another variable not shown above and didn't realize it could also break the injection.
0

It's a typo.

You defined a reportsService

.factory('reportsService')

But injected a different one

static $inject = ['$stateParams', 'reportService'];

1 Comment

It's true that there is a typo. However, it was made in the question, not in the original code. Otherwise there would be DI error, not ' getReportById is not a function'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.