After receiving assistance on saving JSON as a file on the client side from this source, I managed to create a concise code snippet inside this fiddle.
var link = document.createElement('a');
link.download = "backup.json";
link.href = url;
link.textContent = "Download backup.json";
document.getElementById('content').appendChild(link);
I attempted to develop an AngularJS directive that would trigger a method in the scope to retrieve data, but encountered some challenges along the way.
module.directive('myDownload', function ($compile) {
return {
restrict:'E',
scope:{ getData:'&getData'},
link:function (scope, element, attributes) {
element.append($compile(
'<a class="btn" download="backup.json"' +
'href=' + scope.getData() + '>' +
'Download' +
'</a>'
)(scope));
}
};
});
I struggled to transform the functionality of the linked fiddle into a working directive. Can you provide any guidance?