impregnable fiend impregnable fiend - 24 days ago 7
AngularJS Question

Filter files in folder and display them using ng-repeat (JS, Angular)

So, I've got some folder

archives
that contains unknown number of files, I know that:
  • all of them got .7z extension
  • half starts with letters AB and other half - CD


  • So folder's content looks like:
  • AB1234567890.7z
  • AB2345678901.7z
  • CD1234567890.7z
  • CD2345678901.7z
  • etc. That is I can do something like this:

    <a href="archives/AB1234567890.7z" download="{{fileName}}">Download</a>


    And on click it's gonna start downloading archive with name
    AB1234567890.7z
    . It's okay, but obviously I can't write links like that, it must be done with
    ng-repeat
    . So the question is - how to display 2 lists of links, where first list would be list with links which starts with
    AB
    and second - which starts with
    CD
    , respectively. Any help will be greatly appreciated :)

    Answer

    What about this:

    angular.module('app',[]).controller('test', ['$scope', '$http', function($scope, $http){  
     //$http({ method: 'GET', url: '/getNames'}).then(function(names) {
     //    $scope.files = names;
     //}) 
      $scope.files = ['AB1', 'AB2', 'CD1', 'CD2'];
      $scope.startWith = function(file, name){    
        return file.indexOf(name) == 0;
      }
    }])
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app='app' ng-controller='test'>
    <p>First list</p>
      <div ng-repeat='file in files' ng-if="startWith(file, 'AB')">
        <a  href='archives/{{file}}'>{{file}}</a>
      </div>      
    <p>Second list</p>
      <div ng-repeat='file in files' ng-if="startWith(file, 'CD')">
        <a  href='archives/{{file}}'>{{file}}</a>
      </div>
    </div>

    Comments