Javascript 将作用域值与筛选器一起使用
我需要一个过滤器,使用ngRepeat过滤表中显示的结果。 我想根据用户输入过滤这些结果 在这一点上,我正在做这样的事情:Javascript 将作用域值与筛选器一起使用,javascript,angularjs,Javascript,Angularjs,我需要一个过滤器,使用ngRepeat过滤表中显示的结果。 我想根据用户输入过滤这些结果 在这一点上,我正在做这样的事情: //HTML <input type="search" ng-model="search"> <table> <tr ng-repeat="person in clients | filter: searchFilter"> <td><input type="text" ng-model="person.V
//HTML
<input type="search" ng-model="search">
<table>
<tr ng-repeat="person in clients | filter: searchFilter">
<td><input type="text" ng-model="person.Vorname"></td>
</tr>
</table>
一切正常。然而,我不确定我是否用正确的方法来做这件事
为了符合标准,是否应该将过滤器功能移动到实际的角度过滤器
如果是这样,我应该如何将值从输入元素传递到过滤器?我知道我可以通过范围本身,但这似乎有点错误/过于依赖范围
这里怎么走
为了符合标准,是否应该将过滤器功能移动到实际的角度过滤器
是的,使用过滤器作为实用工具的更好方法(比如单例作为服务、提供者、工厂)
过滤器不使用$scope
它根据输入创建输出,因此您可以编写如下过滤器:
app.filter('myfilter', function() {
return function( items, types) {
var filtered = [];
angular.forEach(items, function(item) {
// <some conditions>
filtered.push(item); // dummy usage, returns the same object
}
});
return filtered;
};
});
项目结构如下所示:
app/ --> all of the files to be used in production
css/ --> css files
app.css --> default stylesheet
img/ --> image files
index.html --> app layout file (the main html template file of the app)
index-async.html --> just like index.html, but loads js files asynchronously
js/ --> javascript files
app.js --> application
controllers.js --> application controllers
directives.js --> application directives
filters.js --> custom angular filters
services.js --> custom angular services
lib/ --> angular and 3rd party javascript libraries
angular/
angular.js --> the latest angular js
angular.min.js --> the latest minified angular js
angular-*.js --> angular add-on modules
version.txt --> version number
partials/ --> angular view partials (partial html templates)
partial1.html
partial2.html
编写自己的筛选器非常简单:只需在模块中注册一个新的筛选器工厂函数。在内部,它使用filterProvider。此工厂函数应返回一个新的筛选器函数,该函数将输入值作为第一个参数。任何筛选器参数都将作为附加参数传递给筛选器函数-->elclanrs虽然您的评论很有帮助,但我想指出,在这种特定情况下,saif第一个参数将是ngRepeat指令中使用的数组。但是,我发现我可以使用persondata将searchterm作为第二个参数传入:search@maxim舒斯汀:请看这个问题。并阅读plunker html和js中的注释files@ShivekParmar示例是如何引用当前问题的?请把你的问题的链接贴出来,你的答案在哪里problem@MaximShoustin,对此表示歉意,这里是问题的链接。
filters.js --> custom angular filters
app/ --> all of the files to be used in production
css/ --> css files
app.css --> default stylesheet
img/ --> image files
index.html --> app layout file (the main html template file of the app)
index-async.html --> just like index.html, but loads js files asynchronously
js/ --> javascript files
app.js --> application
controllers.js --> application controllers
directives.js --> application directives
filters.js --> custom angular filters
services.js --> custom angular services
lib/ --> angular and 3rd party javascript libraries
angular/
angular.js --> the latest angular js
angular.min.js --> the latest minified angular js
angular-*.js --> angular add-on modules
version.txt --> version number
partials/ --> angular view partials (partial html templates)
partial1.html
partial2.html