Search code examples
angularjsangularjs-scopeangular-ngmodelangularjs-ng-click

auto generate download link with angularjs


i generate my download link from this script

https://github.com/joshpangell/single-use

for example: download url generated like this:

http://cloud.joshpangell.com/singleuse/download.php?key=key580e36b2ce7ff2.31652971&i=0

everytime i generate the key by cronjob saved the key on text.txt like this:

key580e36b2ce7ff2.31652971

so my question How to include this key generated in the button download.

i`m newbie please explain with example.

here is example of my request:

<html ng-app="dApp">
  <head>
    <meta charset="utf-8">
    <title>Download file</title>
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
    <script>
      var dApp = angular.module('dApp', []);
      dApp.controller('dCtrl', function ($scope){
        $scope.dLink = 'text.txt';
        });
    </script>
  </head>
  <body ng-controller="dCtrl">
<a class= 'btn btn-primary' href="'/su/pathproduct/download.php?{dLink}' + '&i=0'">Download</a>

  </body>
</html>    

Solution

  • try this:

    <html ng-app="dApp">
    <head>
      <meta charset="utf-8">
      <title>Download file</title>
      <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"></script>
      <script>
        var dApp = angular.module('dApp', []);
        dApp.controller('dCtrl', function ($scope, $http) {
          // get a key from the text.txt file
          $http.get('text.txt').then(function (response) {
            $scope.dLink = response.data;
          });
        });
      </script>
    </head>
    
    <body ng-controller="dCtrl">
      <a class="btn btn-primary" 
         ng-if="dLink" 
         ng-href="/su/pathproduct/download.php?key={{ dLink }}&i=0'">Download</a>
    
    </body>
    </html>  
    
    1. added $http request which fetches a key from the text file and then sets it to or scope variable
    2. added ng-if to the link, so the link is not visible while dLink is empty