[英]Controller method on button click of custom directive ? angular js
I have my custom directive to upload file like this in parent HTML 我有我的自定义指令,可以在父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>
In parent controller the code for deleteAction is 在父控制器中,deleteAction的代码为
$scope.deleteImage = function (attachment) {
alert(attachment.id);
}
my uploadAllAction is working now I want to implement deleteAction in a similar way here is its implementation 我的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
};
Now I want to initialize my method 现在我想初始化我的方法
<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>
So ng-click="delete(item)"
in directive used to initialize the deleteAction 因此,指令中的ng-click="delete(item)"
用于初始化deleteAction
scope.delete = function (attachment) {
debugger;
scope.deleteAction(attachment); //error delete action is not defined
}
when I click delete button the directive js initialize the deleteAction with attachment and it should call controller method then defined in directive but I get the error 当我单击删除按钮时,指令js初始化带有附件的deleteAction,它应调用控制器方法,然后在指令中定义,但出现错误
TypeError: scope.deleteAction is not a function TypeError:scope.deleteAction不是函数
You should passs the deleteAction as an action binding `deleteAction: '&', and then you can do something like 您应该将deleteAction作为一个绑定为`deleteAction:'&'的动作传递,然后您可以执行类似的操作
<button class="info" ng-click="deleteAction({item})">x</button>
And as mentioned I would recommend to avoid using two-way bindings when creating components. 如前所述,我建议在创建组件时避免使用双向绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.