Thursday, October 19, 2017

Controller method on button click of custom directive ? angular js

I have my custom directive to upload file like this in parent 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

  $scope.deleteImage = function (attachment) {

        alert(attachment.id);
   }

my uploadAllAction is working now i want to implement deleteAction in similar way here is its implementation

         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

    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 error

TypeError: scope.deleteAction is not a function

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/10/19/controller-method-on-button-click-of-custom-directive-angular-js/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment