2015-10-27 212 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

相關問題