user6882159
user6882159

Reputation:

passing values to controller from directive

I am creating a web app in which i created a directive for input type file

app.directive('customFileInput', [function () {
        return {
            link: function (scope, element, attrs) {
                element.on('change', function (evt) {
                    var files = evt.target.files;
                    scope.filename = files[0].name
                    console.log(scope.filename);
                });
            }
        }
    }]);

this is my directive for fetching the file name from

<input id="file" type="file" ng-model="mdfile" ng-change="filepath()" class="form-control"  custom-file-input />

this input field

now in my controller i want to pass the filename which i get from the directive

$scope.savefunction = function () {
            var f = document.getElementById('file').files[0],
                r = new FileReader();
            r.onloadend = function (e) {
                $scope.data = e.target.result;
            }
            r.readAsDataURL(f);

            $http.get('/employeeregistration.asmx/sav', {
                params: {
                    data: $scope.data,
                    //i want to pass the value here
                }
            }).then(function (response) {

            })
        }

how i can pass the value

Upvotes: 1

Views: 1602

Answers (1)

Avnesh Shakya
Avnesh Shakya

Reputation: 3906

Use scope: false in DDO

return { 
  restrict: “EA”, 
  scope: false, 
  link: function(scope, elem, attr){ 
   // write code here. 
  } 
} 

name filename same in both place. example

OR

Use isolated scope:

return {
    restrict: "EA",
    scope: {
        filename: "="
    },

see isolated example Hope this will help.

Upvotes: 1

Related Questions