Luca Morelli
Luca Morelli

Reputation: 2560

cannot change the syntax of the controller creation in typescript

I have an angular controller wrote in typescript and declared this way

angular.module('myApp').controller("gestioneProgetto",     
 ["$scope", "PalmariService", "SoluzioniService", "ProgettoService", "WmsService", "BingService", "$modal", "SettoreService",  
  ($scope, dispositivi, soluzioni, progetti, wms, bing, modal, settore)     
=> new Palmare.Controllers.gestioneProgetto($scope, dispositivi, soluzioni, progetti, wms, bing, modal, settore)])

and is working fine. I want to change removing from here the di references, moving to

static $inject = ["$scope", "PalmariService", "SoluzioniService", "ProgettoService"
                 , "WmsService", "BingService", "$modal", "SettoreService"]; 

and changing to

angular.module('myApp').controller("gestioneProgetto", ($scope, dispositivi, soluzioni, progetti, wms, bing, modal, settore)
    => new Palmare.Controllers.gestioneProgetto($scope, dispositivi, soluzioni, progetti, wms, bing, modal, settore));

this approach worked fine with all the factoriesI changes, but trying with the first controller doing this I obtain the message

Error: [$injector:unpr] Unknown provider: dispositiviProvider <- dispositivi

Am I missing something?

Upvotes: 1

Views: 45

Answers (2)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220964

To add to Radim's correct answer, the reason this is happening is because Angular is looking for the $inject property on the object that is passed as the second argument to controller.

In this case, the argument you have here:

angular
    .module('myApp')
    .controller("gestioneProgetto", ($scope, ... settore) => new Palmare.Controllers.gestioneProgetto($scope, ... settore));

is an anonymous function that doesn't have a $inject property. If you pass the controller constructor function itself, that constructor function does have the $inject property (because static members of a class appear as properties on the constructor function itself).

Upvotes: 1

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

In case your definition is like this:

module Palmare.Controllers
{
    export class gestioneProgetto
    {
        static $inject = ["$scope", "PalmariService", "SoluzioniService", ...]; 
        ...

This should work for angular:

angular
  .module('myApp')
  .controller("gestioneProgetto", Palmare.Controllers.gestioneProgetto);

Upvotes: 1

Related Questions