javascript - 通过filter返回一个数组

标签 javascript angularjs

我有一个过滤器

angular.module('filters', []).filter('check', function(data) 
{
  return function(name)
   {
     var a=new Array();
     a[0]="hello";
     a[1]="hi";
     return a;
   };
}); 

如您所见,过滤器返回一个数组。我如何遍历模板(html页面)中的内容。

{{name|check:data}}

我正在调用过滤器,它返回的结果为:["hello","hi"]

如何遍历模板中的数组。请帮忙。

最佳答案

您可以在 ng-repeat 中使用过滤器。

演示:http://jsfiddle.net/sunnycpp/YbdXQ/7/

<div ng-controller="myCtrl">
    <input type="text" ng-model="strInput" >
    <ul>
        <li ng-repeat="text in strInput|split">
            {{text}}
        </li>
    </ul>
</div>


var angModule = angular.module('myApp', []);
angModule.filter('split',function() {
    return function(strData) {
        return strData.split(" ");
    }
});

angModule.controller("myCtrl", function($scope) {
    $scope.strInput = "Provide your input here";
});

关于javascript - 通过filter返回一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14473572/

相关文章:

javascript - 捕获 ng-show 事件并聚焦输入字段

javascript - react : Avoid nondeterministic first render (hack with "mounted" state)

javascript - 在不超过堆栈限制的情况下迭代或递归大量庞大函数的最佳方法是什么?

javascript - jQuery 删除 SlideUp 上的类

javascript - 为什么我的全局变量在发布请求后不覆盖

angularjs - Angular $http.put REST

javascript - Angular 嵌套指令

javascript - node.js:创建通过数据驱动事件自动更新客户端的应用程序

javascript - 正则表达式(或技术)从完整的歌曲名称中猜测艺术家?

css - 在angular js中用空格加入逗号