Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从指令在控制器内执行javascript函数_Javascript_Angularjs - Fatal编程技术网

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

从指令在控制器内执行javascript函数,javascript,angularjs,Javascript,Angularjs,我需要执行驻留在控制器中的javascript函数。我需要从指令中调用函数 我传递的论点很好。我在控制器中的方法名是“GetAttachments” 在调试和使用范围时,方法名GetAttachments不会出现 有人能帮我执行指定的函数吗 这是我的指示。我需要知道行的正确语法:scope.GetAttachments(attrs.downloadType,attrs.downloadId)。请注意,我的论点很好 .directive('download', ['$modal', functio

我需要执行驻留在控制器中的javascript函数。我需要从指令中调用函数

我传递的论点很好。我在控制器中的方法名是“GetAttachments”

在调试和使用范围时,方法名
GetAttachments
不会出现

有人能帮我执行指定的函数吗

这是我的指示。我需要知道行的正确语法:
scope.GetAttachments(attrs.downloadType,attrs.downloadId)
。请注意,我的论点很好

.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)
            }

        }
    }
}])
这是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>

文件下载
  • {{item.ExternalDocumentId} 下载
接近
您需要通过作用域在控制器中公开函数GetAttachments

而不是

viewModel.GetAttachments = function (CheckID, FileID)
试试这个:

$scope.GetAttachments = function (CheckID, FileID)
但是请注意,只有当指令和控制器共享作用域时(我认为您的代码就是这样),这才有效。但是,如果您想使用指令的独立作用域(为了更好的模块化和可重用性),那么您必须使用'&'绑定将方法的引用(在控制器中)传递到指令的作用域


查看这个“小提琴手”,从指令“

”中获得隔离范围和调用函数的演示。您可能需要考虑使用“

”之类的方法在指令中引发事件。
$rootScope.$broadcast('myEventHappened');
$scope.$on('myEventHappened', function() {});
然后在你的控制器中,用类似于:

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

这种方法将防止您将指令与控制器紧密耦合

可能的副本不起作用。。。。在我将它改为$scope而不是viewModel之后,它没有进入方法内部……然后我认为您的指令和控制器没有共享相同的作用域。您可以尝试使用隔离作用域吗?你能显示你的HTML吗?我包括了HTML。我不需要使用隔离作用域,因为我传递给函数的参数可以通过attrs属性访问。您在HTML中的何处使用了下载指令?