2017-10-30 20 views
0

UIでファイルのアップロードの成功または失敗にメッセージを表示する。 私は次のファイル構造を持っています。angularJSのサービスからスコープ変数を使用する方法

コントローラー:

 $scope.uploadFile = function() { 
        //using a service 
        fileUpload.uploadFileToUrl(file, uploadUrl) 
    }; 

これは完全に正常に動作します:

マイファイルのアップロード機能は、このようなものです。 ファイルアップロードサービスは、次のようになります。

 angular.module('myApp').service('fileUpload', ['$http', function 
    ($http) { 


     //upload function happens here and returns a promise, This is  executing fine. 
      .success(function (response) { 
       if (response.status=="uploadSuccess") 
       { 
        alert("The file has been successfully uploaded"); 
        var message = "success"; 
             } 
       if (response.status == "uploadFailure") { 
        alert("The file could not be uploaded"); 

             } 
       if (response.status == "uploadFailureExc") { 
        alert("The file could not be uploaded due to an exception that occured"); 


       } 
       if (response.status == "FileExists") { 
        alert("The file could not be uploaded - a file with that name already exists"); 

             } 


      }) 
      .error(function() { 

      }); 

    } 
     }]); 

どのように私の代わりにアラートを使用しての私のhtmlページにメッセージを表示することができます。私は変数varメッセージを設定しようとしました。それをサービスから戻しますが、無限ループに陥ってしまいました。スコープ変数を使用しようとしましたが、その変数も無限ループに入ります。応答が利用可能になったとき

ヘルプ

+0

この投稿で受け入れられる回答を見てください。https://stackoverflow.com/questions/28382927/listen-to-window-events-in-an-angularjs-service質問はあなたと同じではありませんでしたが解決策あなたのために働くでしょう。 – jbrown

答えて

1

$httpは、非同期コールバックをトリガします。 あなたは

fileUpload.uploadFileToUrl(file, uploadUrl).then(function(data) { 
    console.log(data); 
}); 
+0

私はこれを試しました。これをデバッグすると、successCallbark関数またはerrorCallback関数の内部には入りません。 UI上の私のメッセージアラートは空白のままです。 – Pares

+0

'.then'はサービス内で動作しましたか? – Hareesh

+0

はい、それはサービスの内部で働いていました。私はそのサービスの中でメッセージを得ています。 – Pares

0
.success(function (response) { 
     if (response.status=="uploadSuccess") 
     { 
      var message = "The file has been successfully uploaded"; 
      return message; 
    } 
     if (response.status == "uploadFailure") { 
      var message = "The file could not be uploaded"; 
      return message; 
          } 
     if (response.status == "uploadFailureExc") { 
      var message = "The file could not be uploaded due to an exception that occured"; 
      return message; 

     } 
     if (response.status == "FileExists") { 
      var message = "The file could not be uploaded - a file with that name already exists"; 
      return message; 

           } 


    }) 

アップロード時にすべての条件のためのメッセージを返します。このコードで成功ブロックに置き換え、あなたのサービス

.then(function successCallback(response) { 
    // this callback will be called asynchronously 
    // when the response is available 
}, function errorCallback(response) { 
    // called asynchronously if an error occurs 
    // or server returns response with an error status. 
}); 

から、あなたのコントローラでの応答を取得するために.thenを使用することができますし、今すぐあなたが望む方法であなたのコントローラでそれを使用することができます。お役に立てれば。!

+0

私はこれを試しました。 $ scope.messagedisplay = fileUpload.uploadFileToUrl(file、uploadUrl) 私は$ scope.messagedisplayを定義されていません。 – Pares

+0

@Paresは、fileUpload.uploadFileToUrl(file、uploadUrl).then(function(response){ $ scope.messagedisplay = response; }を使用してメッセージ表示に値を割り当てます) – amanpurohit

関連する問題