1

I have JS code with Angular controller which starts like this:

angular.module('myApp.controllers', [])
    .controller('BookmarksController', function($scope, bookmarkService, crawlerService) {

I want to rewrite it using TypeScript. Here is my code:

class Some {

    constructor($scope, bookmarkService, crawlerService) {} 
}

angular
    .module('myApp.controllers', [])
    .controller('BookmarksController', Some($scope, bookmarkService, crawlerService));

It doesn't work:

Can not find name $scope, bookmarkService, crawlerService

How can I fix it ?

1 Answer 1

2

The best way (well, solution ready for minification) would be:

class Some {
    // instruction for IoC
    static $inject = ["$scope", "bookmarkService", "crawlerService"];

    constructor($scope, bookmarkService, crawlerService) {} 
}

angular
    .module('myApp.controllers', [])
    .controller('BookmarksController', Some);

Where we are using the static $inject = [] array for IoC names, which will work even after minification. Then we just register the controller class

.controller('BookmarksController', Some);

and later angular IoC will properly find out what should be injected as constructor params

In case we would use some modules/namespaces

module My.Namespace
{
    class Some {

we would use:

.controller('BookmarksController', My.Namespace.Some);
1

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.