0

I know that there are a lot of these question, but my is specific. I am using Typescript with AngularJS. I want to get variable from multimediaController to multimediaAlbumController. I am getting "[$injector:unpr] Unknown provider: multimediaControllerProvider <- multimediaController <- multimediaAlbumController". How can i prevent it?

MultimediaAlbumController

export class MultimediaAlbumController{
    $scope;
    albums        : AlbumDto[];
    $popupManager;
    $state;
    $element;
    mutlimediaController;
    static $inject = ['$injector', '$scope', '$stateParams', '$state', '$popupManager', '$element','multimediaController']
    constructor(
            $injector         : ng.auto.IInjectorService,
            $scope,
            $stateParams,
            $state,
            $popupManager,
            $element,
            mutlimediaController: MultimediaController
        ) {
        super();
        $injector.invoke(this.init, this, {$scope});
        this.$scope = $scope;
        this.$element = $element;
        this.$state = $state;
        this.$popupManager = $popupManager;
        this.mutlimediaController = MultimediaController;
        this.albums = mutlimediaController.albums;
}

As you can see i have declared multimediaController, even write it in $inject. So where is the bug? :/

  • It seems that the `multimediaController` isn't registered by your module, so it doesn't know what to inject there – devqon Apr 07 '17 at 07:52
  • But i discovered that i cannot inject Controller to Controller, so i am doing it wrong :( Any ideas, how can i get this variable? –  Apr 07 '17 at 08:07

1 Answers1

2

You should not inject one controller into another rather you should be using service\factory.

However to get the scope of mutlimediaController, you need to inject $controller service of angularjs. You can do it ike :

static $inject = ['$injector', '$scope', '$stateParams', '$state', '$popupManager', '$element','$controller']

    constructor(
            $injector: ng.auto.IInjectorService,
            $scope,
            $stateParams,
            $state,
            $popupManager,
            $element,
            $controller
        ) {
        super();
        $injector.invoke(this.init, this, {$scope});
        this.$scope = $scope;
        this.$element = $element;
        this.$state = $state;
        this.$popupManager = $popupManager;
        var mutlimediaController  = $controller('MultimediaController', {this.$scope: $scope});
        mutlimediaController.someFunction();// some function of your MultimediaController
        this.albums = mutlimediaController.albums;
}

But I would suggest making service/factory to access the common data.

Like :

myApp.service('myService', function() {
    var text = '';
    this.getValue = function () { 
      return text;
    }
    this.setValue = function (data) { 
     return text = data;
    } 
});

//Inside controller    
myApp.controller('MyCtrl', function MyCtrl($scope, myService) {
myService.setValue ("data to service");
$scope.message = myService.getValue();
});
anoop
  • 3,812
  • 2
  • 16
  • 28
  • Hmmm.. have done like you wrote it and i am getting The controller with the name 'MultimediaController' is not registered –  Apr 07 '17 at 08:51
  • Anyway, it's ok. Thought that i can access that data from antoher controller ( i have service, but i thought that i shouldn't shoot again for data from database ). –  Apr 07 '17 at 08:52
  • I hope\assume you have registered your controller like `export class MultimediaController { //controller code } app.controller('MultimediaControllerl', MultimediaControllerl);` but I would suggest to utilize your service. – anoop Apr 07 '17 at 08:56