AngularJS 未知提供商 - 对于控制器

AngularJS Unknown provider - For Controllers

我知道有很多这样的问题,但我的问题很具体。我在 AngularJS 中使用 Typescript。我想从 multimediaController 获取变量到 multimediaAlbumController。我得到“[$injector:unpr] 未知提供者:multimediaControllerProvider <- multimediaController <- 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;
}

如你所见,我已经声明了 multimediaController,甚至将它写在 $inject 中。那么bug在哪里呢? :/

你不应该 inject 一个控制器变成另一个控制器,你应该使用 service\factory

但是要获得mutlimediaControllerscope,您需要注入angularjs的$controller服务。 你可以这样做:

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

但我建议 service/factory 访问公共数据。

喜欢:

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