【问题标题】:Inject Directive into Controller将指令注入控制器
【发布时间】:2016-01-26 18:01:13
【问题描述】:

已解决:

我使用了错误的方法。现在我在指令的链接函数中定义了一个 $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());
}

当我注入工厂时,代码可以正常工作,而不是我无法获得对指令控制器的引用(在主控制器的 aendern 方法中)。当我尝试将指令本身注入主控制器时,我得到一个注入器错误:

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

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

    问题中显示的错误很清楚:

    当 Angular 尝试实例化 CLogic.pps.produktionsAuftragDetailController 并因此注入其依赖项 static $inject = [ 'CLogic.pps.services.ppsDataService' ... 时 service 找不到

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

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

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

    【讨论】:

    • 当我注释掉 ..factory('stListLoeschenFactory', stListLoeschenDirective.factory());行并在主控制器中注入以下内容,错误相同: static $inject = [ '$scope', ... , 'stListLoeschenDirective' ];构造函数(私有 $scope:任何,...,私有 stListLoeschenDirective:CLogic.pps.directives.stListLoeschenDirective)
    • 如果你注释掉..那么错误不会消失,因为它仍然是未知的提供者。所以你必须定义一些......但是......你永远不能将指令传递给控制器​​!绝不。您使用的概念是错误的。换个角度想。指令有一个控制器。还有另一个控制器(其他指令或视图)。他们可以提供一些工厂/服务..来更改数据。但是控制器永远不能将指令作为依赖项..
    • 我只是说我注释掉了包含 .factory 语句的“错误”行。如果这是错误的方法,我如何从外部调用指令控制器中的 open() 方法?我认为服务或工厂无法做到这一点?
    • 不,不能。但是您可以将其用作一种如何共享数据的方式。因此,一个控制器可以 $watch 某些服务属性(作为依赖项传递)并执行 OPEN 操作,而其他控制器只是更改该属性...如果您尝试阅读有关如何在指令之间进行通信的更多信息..您应该看到它是通常和常见的方式。希望它有所帮助-阅读此docs.angularjs.org/guide/directive
    • 这真是太棒了。使用 Typescript 享受 Angular ;)
    猜你喜欢
    • 2014-02-21
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多