AngularJS未知提供程序 - 适用于控制器

时间:2017-04-07 07:46:12

标签: angularjs typescript provider

我知道这些问题很多,但我的具体问题。我正在使用带有AngularJS的Typescript。我想从multimediaController到multimediaAlbumController变量。我得到" [$ injector:unpr]未知提供者:multimediaControllerProvider< - multimediaController< - multimediaAlbumController"。我怎么能阻止它?

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在哪里? :/

1 个答案:

答案 0 :(得分:2)

您不应该inject将一个控制器放入另一个控制器,而应该使用service\factory

但是要获得scope的{​​{1}},您需要注入angularjs的mutlimediaController服务。 你可以这样做ike:

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