Kevin Vincent Kevin Vincent - 2 months ago 7
HTML Question

How to create a dynamic tag by using angularJS?

I am trying to create a

<a>
HTML element by using angularJS. I want to do that because I want to download a file from the client available on the server. I saw that the easy way to do this is by using a
<a>
html element with the href attribute. Here is my piece of code :

$http({
url: '/process',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: appData.elements
}).then(function sucessCallback(response) {
//Create a link and emulate a click on it
}, function errorCallback(response) {

});


So I send a request to the server. Then, it builds an android project in the purpose of generating an APK. I want to download this APK from the server by clicking on the
<a>
element.

Answer

Create a dynamic <a> tag,

 $http({
         url: '/process',
         method: 'POST',
         headers: {
             'Content-Type': 'application/json'
        },
            data: appData.elements
        }).then(function sucessCallback(response) {
            //Create a link and emulate a click on it
            var anchor = angular.element('<a/>');
            angular.element(document.body).append(anchor);

            //hide with css
            anchor.css({display: 'none'});

            var body = $document.find('body').eq(0);
            body.append(anchor);
            anchor.attr({
                href: data.content.fileName,
                target: '_blank',
                download: 'filename.doc'
            })[0].click();
            anchor.remove();
        }, function errorCallback(response) {

        });