0

I am trying to post file on my server as soon as it will get attached with directve so I did api call but I got this error while doing. I don't know where am i going wrong

here is my code

app.directive('fileModel',fileupload);

fileupload.$inject =  ['$http','$parse']; 

function fileupload ($http,$parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs,$http) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function(){
                scope.$apply(function($http){
                    modelSetter(scope, element[0].files[0]);
                    var fd = new FormData();
                    fd.append('file', element[0].files[0]);
                    fd.append('id', user_id);
                    var uploadUrl = "https://******/routes/contactRoute.php?action=upload";
                    $http.post(uploadUrl, fd, {
                        transformRequest: angular.identity,
                        headers: {'Content-Type': undefined}
                    }).
                    then(function successCallback(response) {
                        console.log(response)
                      }, function errorCallback(response) {
                        console.log(response)
                    });
                });
            });
        }
    };
}

1 Answer 1

3

Remove $http from link function. it is not needed

app.directive('fileModel',fileupload);

fileupload.$inject =  ['$http','$parse']; 

function fileupload ($http,$parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            var model = $parse(attrs.fileModel);
            var modelSetter = model.assign;

            element.bind('change', function(){
                scope.$apply(function($http){
                    modelSetter(scope, element[0].files[0]);
                    var fd = new FormData();
                    fd.append('file', element[0].files[0]);
                    fd.append('id', user_id);
                    var uploadUrl = "https://******/routes/contactRoute.php?action=upload";
                    $http.post(uploadUrl, fd, {
                        transformRequest: angular.identity,
                        headers: {'Content-Type': undefined}
                    }).
                    then(function successCallback(response) {
                        console.log(response)
                      }, function errorCallback(response) {
                        console.log(response)
                    });
                });
            });
        }
    };
}

Edit 1: You also need to remove $http from $apply callback. You don't need to pass $http to any other internal function. It has been already injected and available for all other internal methods.

scope.$apply(function(){}
Sign up to request clarification or add additional context in comments.

4 Comments

Still same error @Ved $http.post is not a function at summaryController.js:19
@ved answer should also work but needs some tweaking and verifying. Check my answer - notice $http removed from the $scope.apply as well
Thanks @Ved . you made my day.
@sameerdighe Welcome. Happy to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.