2016-07-31 4 views
0

컨트롤러 외부의 텍스트 영역에 값을 전달하거나 설정할 수 없습니다. 엑셀을 업로드하고 업로드 상태와 관련하여 일부 데이터를 텍스트 영역으로 설정하려고합니다.각도 서비스 범위 및 설정 값

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

, 당신은 그 때는 대신 일단 .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(){ 

} 
    }; 
}]);