Search code examples
javascriptangularjsangular-directive

how to get arguments in directive


I have a button and when I click on that function I need to call a directive. For this I have writern the following code.before creating directive there is a ng-change function.I have removed ng-change and kept directive as follows,

<button  upload-file="selectedDocumentName,loanFolderNumber" ><!-- ng-click="uploadFile(selectedDocumentName,FolderNumber)" -->

How can I take that arguments selectedDocumentName,FolderNumber in my directive.I have tried in the following way,but I am not getting the values.

Directive:-

app.directive('uploadFile',['documentService',function(documentService){
    return {
        restrict: 'AE', 
        scope: {
            selectedDocumentName: '=',
            FolderNumber: '=',
        },controller: function($scope){
            $scope.selectedDocumentName;
        },
        link : function($scope,element,attrs){

            element.on('click',function(e){
            })

        }
    }
}]);

Solution

  • Try this

    <button upload-file obj="obj">A</button>
    
        var myApp = angular.module('myApp',[]);
    
    
        myApp.directive('uploadFile', function() {
            return {
                restrict: 'AE', 
                scope: {
                    obj1: '='
                },controller: function($scope){
                    $scope.obj2 = JSON.parse(JSON.stringify($scope.obj1))
                    console.log($scope.obj2.selectedDocumentName);
                    $scope.obj2.selectedDocumentName = "DEF";
                     console.log($scope.obj2.selectedDocumentName);
                },
                link : function($scope,element,attrs){
    
                    element.on('click',function(e){
                    })
    
                }
            }
        });
    
    
        myApp.controller('MyCtrl', function ($scope) {
           $scope.obj = {selectedDocumentName : "ABC",loanFolderNumber : "DEDF"};
        });
    

    Check this fiddle