2016-07-31 7 views
0

コントローラ外のテキスト領域に値を渡したり設定することはできません。 私はExcelをアップロードしており、アップロード状況に関しては、一部のデータをテキスト領域に設定したいと考えています。 これは、これまでの私のコードです:サービスへの角度範囲と設定値

app.service('fileUpload', ['$http', function ($http) { 
    this.uploadFileToUrl = function(file, uploadUrl, commentArea){ 
     var fd = new FormData(); 
     fd.append('file', file); 
     $http.post(uploadUrl, fd, { 
      transformRequest: angular.identity, 
      headers: {'Content-Type': undefined} 
     }) 
     .success(function(){ 
/*   commentArea.append('This is not working'); 
      commentArea = 'This is not working'; 
      $scope.outputImportObject = 'This is not working'; 
*/ 
      alert('The file was succesfully uploaded!'); 
     }) 
     .error(function(){ 
      alert('There was an error while inserting the file!'); 
    }); 
    } 
}]); 

app.controller('UploadCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){ 
    $scope.uploadFile = function(){ 
     $scope.outputImportObject = 'This is working'; 

     var file = $scope.myFile; 
     var commentArea = $scope.outputImportObject; 
     fileUpload.uploadFileToUrl(file, ws_url+'upload_excel.php',commentArea); 
    }; 
}]); 
+0

、あなたが.then代わり.successと.ERRORコールバックを使用する必要があります:

とあなたのコントローラは次のようにする必要があります。もし私が正しいのであれば、彼らは現在廃止されています。より多くのために角度の '$ http' APIを読んでください –

答えて

1

これは通常、あなたが約束を使用する必要がある場合です。 サービスから約束を返す必要があり、解決または拒否に基づいて、変数をコントローラにバインドする必要があります。あなたは直接の代わりにカスタムの約束を作る、それを返すことができ、自身が約束を返すは、httpので

app.service('fileUpload', ['$http', function ($http) { 
    this.uploadFileToUrl = function(file, uploadUrl, commentArea){ 
     var fd = new FormData(); 
     fd.append('file', file); 
     return 
     $http.post(uploadUrl, fd, { 
      transformRequest: angular.identity, 
      headers: {'Content-Type': undefined} 
     }) 
    } 
}]); 

あなたのサービスは次のようになります。また

app.controller('UploadCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){ 
    $scope.uploadFile = function(){ 
     $scope.outputImportObject = 'This is working'; 

     var file = $scope.myFile; 
     var commentArea = $scope.outputImportObject; 
     fileUpload.uploadFileToUrl(file, ws_url+'upload_excel.php',commentArea) 
.then(doThisOnSuccess, doThisOnFailure); 

function doThisOnSuccess(){ 

code for binding to text area should go here 
} 

function doThisOnFailure(){ 

} 
    }; 
}]); 
関連する問題