2016-11-11 46 views
0

所以,我有含有未知数文件的某个文件夹archives显示他们,我知道:过滤文件,并使用NG-重复(JS,角)

  • 所有的人都得到了.7z压缩扩展
  • 一半用字母AB和另一半开始 - CD

  • 那么文件夹的内容是这样的:
  • AB1234567890.7z
  • AB2345678901.7z
  • CD1234567890.7z
  • CD2345678901.7z
  • 等。也就是说,我可以做这样的事情:

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

    并点击它会开始下载压缩文件名称为AB1234567890.7z。没关系,但显然我不能写这样的链接,它必须用ng-repeat完成。所以,问题是 - 如何显示的链接,其中第一清单将是列表的链接与AB开始2只列出了 - 这与CD开始,分别。任何帮助,将不胜感激:)

    +0

    你能分享一下你试过的代码吗?时间?这会更好地阐明你的问题。它有点令人困惑 –

    +0

    @Manoj Shevate我自己不知道如何执行 - 这就是为什么我在这里 - 寻找更有经验的家伙的提示:)据我所知,它可以像'$ scope.firstList = files.filter((文件)=> file.substring(0,2)===“AB”'然后'ng-repeat ='file in firstList' ... –

    回答

    2

    这个怎么样:

    angular.module('app',[]).controller('test', ['$scope', '$http', function($scope, $http){ 
     
    //$http({ method: 'GET', url: '/getNames'}).then(function(names) { 
     
    // $scope.files = names; 
     
    //}) 
     
        $scope.files = ['AB1234567890.7z', 
     
            'AB2345678901.7z', 
     
            'CD1234567890.7z', 
     
            'CD2345678901.7z']; 
     
        $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>

    +0

    找好一个网址,但我怎么在控制器宣布'$ scope.files'? –

    +0

    想这必须由服务器端从文件夹中完成了...我的意思是,名单... –

    +0

    @impregnablefiend,您可以使用,例如,'$ http'服务从服务器获取名称,然后分配返回名称'$ scope.files'变量,在我的情况。 –

    0

    我认为这将有助于你

    <div ng-controller="AppCtrl" layout="row" ng-cloak="" ng-app="MyApp"> 
        <div layout="column" flex> 
         <a ng-repeat="file in archivesAB" href="archives/{{file}}" download="{{file}}">{{file}}</a> 
        </div> 
        <div layout="column" flex> 
         <a ng-repeat="file in archivesCD" href="archives/{{file}}" download="{{file}}">{{file}}</a> 
        </div> 
    </div> 
    
    (function() { 
        'use strict'; 
        angular 
        .module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache']) 
        .controller('AppCtrl', AppCtrl); 
    
        function AppCtrl ($scope, $log) { 
        $scope.archivesAB = [ 
         'AB1234567890.7z', 
         'AB2345678901.7z' 
        ]; 
    
        $scope.archivesCD = [ 
         'CD1234567890.7z', 
         'CD2345678901.7z' 
        ]; 
    
        } 
    })(); 
    

    这里是工作codepen

    +0

    我不能在控制器中打印两个集合 - 它们是未知的! ...他们可以改变任何时间 - 就像我说我知道他们在文件夹'档案'和一些以'AB'开始,其他 - 'CD' ... –

    +0

    据我所知,你不能。使用客户端JavaScript作为AngularJS但是,如果你有这样的文件在你的服务器,你可以只访问的URL返回列表供您阅读从一个文件夹中的文件 – Sanfelice

    +0

    你可以使用这样的事情: $超时(函数() $ http.get('url')。success(function(respon) se){ $ scope.archivesAB = response.archivesAB; $ scope.archivesCD = response.archivesCD; }); 5000); 这将访问返回您列出每个5S – Sanfelice