Download json using Angularjs
<p>I have webapi returning me data and that data i need to download in a txt file.
I have done it using below code and i am getting result as desired. I am somehow looking for a better approach .Is there any angular way to do this ?  </p>

<pre><code>$scope.downloadResponseAsInfoFile = function (response) {
    var link = document.createElement("a");
    link.download = "info.txt";
    var data = "text/json;charset=utf-8," + encodeURIComponent(response);
    link.href = "data:" + data;
    link.click();
};
</code></pre>

<p>how can i handle exception in case i dont get response from server.</p>