2

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 :)

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

Comments

1

As long as your naming is consistent you use this:

HTML:

<div ng-repeat="a in array">
    <a href="test/data_{{$index+1}}.zip" download="{{fileName}}{{$index+1}}"> Download version {{$index + 1}} </a>
</div>

Controller:

var numberOfFiles = 12;
$scope.array = new Array(numberOfFiles);
$scope.fileName = 'myData';

You are creating "n" number of download links and only replacing the number at the end of the file. e.g. test/data_#.zip

Fiddle: http://jsfiddle.net/Lvc0u55v/5812/

2 Comments

Man, thank you too, it works for me. But @chresse was first :)
No problem, use whatever suits your application best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.