2015-10-27 122 views
2

求解:注入控制器指令

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

Orginial问:

我目前工作的一个角与应用程序开发的打字稿。现在我不得不面对以下问题,我有一个指令,其中包含一个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()); 
} 

当我注入的工厂代码工作,而不是事实,我可以不参考指令的控制器(在aendern主控制器的方法)。当我尝试将指令自身注入到主控制器我得到一个注射器错误:

Error: [$injector:unpr] Unknown provider: stListLoeschenDirectiveProvider <- stListLoeschenDirective <- CLogic.pps.produktionsAuftragDetailController http://errors.angularjs.org/1.4.1/ $injector/unpr?p0=stListLoeschenDirectiveProvider%20%3C-tListLoeschenDirective%20%3C-%20CLogic.pps.produktionsAuftragDetailController at https://localhost:44302/Scripts/angular.js:68:12

回答

0

中的问题显示的错误是明确的:你没有配置

when angular tries to instantiate CLogic.pps.produktionsAuftragDetailController and therefore inject its dependency static $inject = [ 'CLogic.pps.services.ppsDataService' ... that service cannot be found

看来,你的工厂正确。

上面的代码是配置它的方式与一样配置指令。这可能是问题:

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

以及当我注释掉..factory('stListLoeschenFactory',stListLoeschenDirective.factory());线和注入在主控制器以下,误差是相同的: 静态$注入= [ '$范围', ... 'stListLoeschenDirective' ]; 构造函数( 私人$范围:任何, ... ,私人stListLoeschenDirective:CLogic.pps.directives.stListLoeschenDirective ) – Hejo

+0

如果您注释掉..。那么错误不能消失,因为它还是老样子是未知的提供。所以你必须定义一些...但是......你永远不能把指令传给控制器!决不。你使用的概念是错误的。以不同的方式考虑它。 Directive有一个控制器。还有另一个控制器(其他指令或视图)。他们可以提供一些工厂/服务来改变数据。但控制器永远不会得到指令作为依赖.. –

+0

我只是说我评论了包含.factory语句的“错误”行。 如果这是错误的做法,我怎样才能从外部调用指令的控制器中的open()方法?我认为服务或工厂无法做到这一点? – Hejo

相关问题