按钮上的控制器方法点击自定义指令?角js

时间:2017-10-19 05:53:28

标签: javascript jquery .net angularjs angularjs-directive

我有自定义指令在父HTML中上传这样的文件

<uploading-multiple-files controlmodel="serviceProfile.serviceProfileAttachments"
   upload-all-action="uploadAll"  delete-action="deleteImage"
   attachment-type-id="scopeTypeId" url="scopeUrl" enable-upload="enableUpload">
</uploading-multiple-files>

在父控制器中,deleteAction的代码是

  $scope.deleteImage = function (attachment) {

        alert(attachment.id);
   }

我的uploadAllAction正在运行我想以类似的方式实现deleteAction这里是它的实现

function uploadingMultipleFiles(FileUploader, $rootScope, $http, $filter, $window, browser) {
    return {
        replace: false,
        scope: {
            'attachmentTypeId': '=?',
            'controlmodel': '=',
            'url': '=',
            'maxsize': '=?',
            'uploadAllAction': '=?',
            'deleteAction': '=?',
            "counter": '=?',
            "enableUpload": '=?',
            "isRequired": '=?'
        },
        templateUrl: '/app/views/Controls/UploadingMultipleFilesDirective/UploadingMultipleFilesDirectiveTemplate.html',
        link: link
    };

现在我想初始化我的方法

  <div class="col-md-6" ng-repeat="item in controlmodelFiltered">
        <div class="col-md-12">
            <button class="info" ng-click="delete(item)">x</button>        
        </div>  
  </div>

因此ng-click="delete(item)" in指令用于初始化deleteAction

    scope.delete = function (attachment) {
        debugger;
        scope.deleteAction(attachment);    //error delete action is not defined           
    }

当我单击删除按钮时,指令js使用附件初始化deleteAction,它应该调用控制器方法然后在指令中定义 但我收到了错误

  

TypeError:scope.deleteAction不是函数

1 个答案:

答案 0 :(得分:0)

您应该将deleteAction作为动作绑定`deleteAction:&#39;&amp;&#39;传递,然后您可以执行类似

的操作
<button class="info" ng-click="deleteAction({item})">x</button> 

如前所述,我建议在创建组件时避免使用双向绑定。