impregnable fiend impregnable fiend - 5 months ago 13
AngularJS Question

How to automatize uploading files process in AngularJS?

I'm just learning so forgive me if my question is transparent. Now I've got in controller:

$scope.fileUrl = 'test/data_1.zip';
$scope.fileName = 'myData';


In html:

<a href="{{fileUrl}}" download="{{fileName}}"> Download version 1 </a>


So, maybe tomorrow I'll add in folder test file data_2.zip... and then over and over again. Hence I will be forced to write almost the same line in the html & controller many times... How can I automatize that process? I hope what I have requested is possible, any help will be appreciated :)

Answer

you can store all your files in an array and iterate overthem with ng-repeat:

$scope.files = [{
        url: 'test/data_1.zip',
        name: 'myData'
    }, {
        url: 'test/data_2.zip',
        name: 'myData2'
    }, ... ];

in your html you write:

<a href="{{file.url}}" download="{{file.name}}" ng-repeat="file in files"> Download version 1 </a>
Comments