2017-07-11 11 views
0

私はサーバにファイルを投稿しようとしていますので、私はAPIコールをしましたが、このエラーが発生しました。私は間違って

をつもりですどこ私はここlink関数から$httpを削除し、私のコード

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) 
        }); 
       }); 
      }); 
     } 
    }; 
} 

答えて

3

あるかわかりません。それは

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) 
        }); 
       }); 
      }); 
     } 
    }; 
} 

編集1必要ありません:あなたはまた、$applyコールバックから$httpを削除する必要があります。他の内部関数に$ httpを渡す必要はありません。それはすでに注入されており、他のすべての内部方法にも利用可能です。

scope.$apply(function(){} 
+1

まだ同じエラー@Ved $ http.postはsummaryController.jsでの関数 ではありません:19 –

+0

@sameerdighe checkthe更新答え – Ved

+0

@ved答えも動作しますが、いくつかの調整と検証が必要である必要があります。私の答えをチェックしてください - $ scope.applyから$ httpを削除しました。 – bhantol

関連する問題