简体   繁体   English

从指令执行控制器内的javascript函数

[英]Execute a javascript function within a controller from a directive

I need to execute a javascript function that resides inside a controller. 我需要执行一个驻留在控制器内的javascript函数。 I need to call the function from within a directive. 我需要在一个指令中调用该函数。

The arguments I'm passing are fine. 我通过的论点很好。 My method name in the controller is "GetAttachments". 我在控制器中的方法名称是“GetAttachments”。

When I'm debugging, and using scope, the method name GetAttachments doesn't appear. 当我正在调试并使用范围时,方法名称GetAttachments不会出现。

Can someone please help me to be able to execute the named function? 有人可以帮助我能够执行指定的功能吗?

Here is my directive. 这是我的指示。 I need to know the proper syntax of the line: scope.GetAttachments(attrs.downloadType, attrs.downloadId) . 我需要知道该行的正确语法: scope.GetAttachments(attrs.downloadType, attrs.downloadId) Note that my arguments are fine... 请注意,我的论点很好......

.directive('download', ['$modal', function ($modal)
{
    return {
        restrict: 'E',
        transclude: false,
        replace: true,
        template: '<a style="padding-right: 5px; color:#fff !important;" class="pull-right" href="#" ng-click="opendownload()"><i class="fa fa-files-o fa-lg" style="padding-right: 5px"></i>Download</a>',
        link: function (scope, elem, attrs, controller)
        {
            scope.opendownload = function ()
            {
                $modal.open({
                    templateUrl: root + 'AccountingModule/modal/attachment/download-modal.html',
                    size: 'md',
                    backdrop: true,
                    controller: 'downloadSPDocumentsController as downloadCtrl',
                    resolve: {
                        attributes: function () { return attrs; },
                    }
                });
                scope.GetAttachments(attrs.downloadType, attrs.downloadId)
            }

        }
    }
}])

Here is my JS function inside the controller: 这是我在控制器中的JS函数:

module.controller('downloadSPDocumentsController', ['$scope', '$http', '$modalInstance', '$location', '$window', 'attributes',
    function ($scope, $http, $modalInstance, $location, $window, attributes)
    {
        var viewModel = this;
        viewModel.attributes = attributes;

        var DocumentDownloadarr;

        viewModel.GetAttachments = function (CheckID, FileID)
        {

Here is the HTML 这是HTML

<!--<p>For Testing Purpose: Download Type: {{downloadCtrl.attributes.downloadType}}</p>
<p>For Testing Purpose: ID: {{downloadCtrl.attributes.downloadId}}</p>-->

<div class="modal-header">
    <h3 class="modal-title">File Download</h3>
</div>

<div class="modal-body" cg-busy="{promise:downloadCtrl.promise}">
    <ul ng-init="downloadCtrl.Init()" class="list-unstyled">
        <li ng-repeat="item in downloadCtrl.DocumentDownloadarr">
            <div class="col-sm-12">
                <div class="form-group">
                    <div class="col-sm-10">
                        <input type="text" class="form-control" ng-value="item.FileDescription" ng-readonly="true" />{{item.ExternalDocumentId}}
                    </div>
                    <div class="col-sm-2">
                        <button type="button" class="btn btn-default" ng-click="downloadCtrl.DownLoadAttachment(item.ExternalDocumentId, item.FileDescription)">Download</button>
                    </div>
                </div>
            </div>
        </li>
    </ul>
</div>

<div class="modal-footer">
    <div class=" btn-toolbar pull-right" role="toolbar">
        <!--<div class="btn-group" role="group">
            <button type="button" class="btn btn-default" ng-click="downloadCtrl.GetAttachments(downloadCtrl.attributes.downloadType, downloadCtrl.attributes.downloadId)">List Attachments</button>
        </div>-->
        <div class="btn-group" role="group">
            <button type="button" class="btn btn-default" ng-click="$close()">Close</button>
        </div>
    </div>
</div>

You need to expose your function GetAttachments in the controller via the scope. 您需要通过作用域在控制器中公开函数GetAttachments

Instead of 代替

viewModel.GetAttachments = function (CheckID, FileID)

try this: 试试这个:

$scope.GetAttachments = function (CheckID, FileID)

However please note that this will work only if the directive and the controller is sharing the scope (which I think is the case on your code). 但是请注意,这仅在指令和控制器共享范围时才有效(我认为代码就是这种情况)。 However if you want to use isolated scope (for better modularity and re-usability) of the directive , then you will have to pass a reference of the method (in your controller) to the directive's scope using '&' binding. 但是,如果要使用指令的隔离范围(为了更好的模块性和可重用性),则必须使用'&'绑定将方法的引用(在控制器中)传递给指令的范围。

Take a look at this fiddler here for a demo of isolated scope and calling function in controller from directives 这里看看这个小提琴手,从指令中获取控制器中隔离范围和调用功能的演示

You might want to consider raising an event in your directive using something like: 您可能需要考虑使用以下内容在指令中引发事件:

$rootScope.$broadcast('myEventHappened');

And then in your controller listen for it with something like: 然后在你的控制器中用以下内容来监听它:

$scope.$on('myEventHappened', function() {});

This approach will keep you from tightly coupling your directive to your controller. 这种方法可以防止您将指令与控制器紧密耦合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM