将指令注入控制器

时间:2015-10-27 10:54:11

标签: angularjs typescript

求助:

我使用了错误的方法。现在我在指令的link函数中定义了一个$ watch,它调用指令控制器上的open()方法。

原始问题:

我目前正在开发使用Typescript开发的Angular应用程序。现在我必须遵循问题,我有一个包含KendoUI窗口作为弹出窗口的指令。指令中的这个窗口应该从主窗体的控制器打开。

控制器:

module CLogic.pps
{
'use strict';
export class produktionsAuftragDetailController
{
    ...
    static $inject = [
        '$scope',
        ...
        //'stListLoeschenWindow'
        ,'stListLoeschenFactory'
        ];      
    constructor(
        private $scope: any,
        ...
        //, private stListLoeschenWindow: CLogic.pps.directives.stListLoeschenDirective
        , private stListLoeschenFactory: CLogic.pps.directives.stListLoeschenDirective
        )
    {

   // Switch Form to Edit Mode
    aendern(stlD: CLogic.pps.directives.stListLoeschenController)
    {
    // Call the open method of the controller of the directive here
        stListLoeschenFactory.controller.open(1234);
    }
    ...
    }
}    
angular
    .module('CLogic.pps')
    .controller('CLogic.pps.produktionsAuftragDetailController',produktionsAuftragDetailController);
}

指令:

module CLogic.pps.directives
{
'use strict';
export class stListLoeschenController
{
    public StListLoeschenWindow: kendo.ui.Window;

    private StListKey: number;public Fixieren: boolean;public Prodinfo: boolean;

    static $inject =
    [
        'CLogic.pps.services.ppsDataService','$log','hotkeys'
    ];
    constructor
        (public dataService: CLogic.pps.ppsDataService, private $log: ng.ILogService, public hotkeys: ng.hotkeys.HotkeysProvider
        )
    {
        this.Fixieren = false;
        this.Prodinfo = false;
    }
    // This is the method that should be called from the main Controller       
    open(index: number)
    {
        this.StListKey = index;
        this.StListLoeschenWindow.open();
        this.StListLoeschenWindow.center();
    }
}

export class stListLoeschenDirective implements ng.IDirective
{
    restrict = 'AE';
    templateUrl = 'CLogic/pps/detail/StListDeleteBestWindow.directive.html';
    controller = CLogic.pps.directives.stListLoeschenController;
    controllerAs = 'stListLoeschen';

    constructor(private ppsDataService: CLogic.pps.ppsDataService, $log: ng.ILogService, hotkeys: ng.hotkeys.Hotkey){}

    link: ng.IDirectiveLinkFn = (scope: ng.IScope, element: ng.IAugmentedJQuery, attributes: ng.IAttributes)=>{ };

    static factory(): ng.IDirectiveFactory 
    {  
        var directive: ng.IDirectiveFactory = (ppsDataService, $log, hotkeys) => new stListLoeschenDirective(ppsDataService, $log, hotkeys);
        directive.$inject = ['CLogic.pps.services.ppsDataService', '$log', 'hotkeys'];
        return directive;
    }
}
angular
    .module('CLogic.pps.directives', [])
    .directive('stListLoeschenWindow', stListLoeschenDirective.factory())
    .factory('stListLoeschenFactory', stListLoeschenDirective.factory());
}

当我注入工厂时,代码工作而不是我不能引用指令的控制器(在主控制器的现代方法中)。当我尝试将指令本身注入主控制器时,我得到一个喷射器错误:

  

错误:[$ injector:unpr]未知提供商:   stListLoeschenDirectiveProvider< - stListLoeschenDirective< -   CLogic.pps.produktionsAuftragDetailController   http://errors.angularjs.org/1.4.1/ $注射器/ unpr?P0 = stListLoeschenDirectiveProvider%20%3C-tListLoeschenDirective%20%3 C-%20CLogic.pps.produktionsAuftragDetailController       在https://localhost:44302/Scripts/angular.js:68:12

1 个答案:

答案 0 :(得分:0)

问题中显示的错误很明显:

  

当angular尝试实例化CLogic.pps.produktionsAuftragDetailController并因此注入其依赖项static $inject = [ 'CLogic.pps.services.ppsDataService'时...无法找到服务

看来,您没有正确配置工厂。

上面的代码以与配置指令相同的方式配置。这可能是个问题:

angular
    .module('CLogic.pps.directives', [])
    .directive('stListLoeschenWindow', stListLoeschenDirective.factory())
    // factory should be different then directive
    .factory('stListLoeschenFactory' , stListLoeschenDirective.factory());