javascript – ng-repeat过滤器类似于Meteor Blaze / Spacebars中的功能
作者:互联网
我来自AngularJS背景,最近开始学习Meteor.
在AngularJS中,我可能有类似的东西:
<div ng-repeat="person in persons | filter:search">
<h4>{{person.name}}</h4>
<b>{{person.age}}</b>
</div>
搜索对象可以绑定(双向绑定)到HTML文本框.只要文本框发生更改,过滤器就会自动更新.
如何在流星中这样做?
解决方法:
我不熟悉AngularJS,但这里有一个如何使用Meteor完成此任务的示例.
此示例显示了一个人员列表,以及可用于按年龄过滤显示列表的HTML编号输入.
客户端/视图/人/ persons.html
<template name="persons">
<input class="age" type="number" value="{{filter}}">
<ul>
{{#each personsFiltered}}
{{> person}}
{{/each}}
</ul>
</template>
<template name="person">
<li>{{name}} is {{age}}</li>
</template>
客户端/视图/人/ persons.js
// dummy collection for testing purpose, living only in the client
// (not backed by a real server-side persistent collection)
Persons=new Mongo.Collection(null);
// dummy dataset
Persons.insert({
name:"Alice",
age:25
});
Persons.insert({
name:"Bob",
age:35
});
Persons.insert({
name:"Charlie",
age:18
});
// on create, initialize our filter as a ReactiveVar
// need to meteor add reactive-var to use this
Template.persons.created=function(){
this.filter=new ReactiveVar(20);
};
Template.persons.helpers({
// value of the filter to initialize the HTML input
filter:function(){
return Template.instance().filter.get();
},
// reactively return the persons who are older than the input value
personsFiltered:function(){
return Persons.find({
age:{
$gt:Template.instance().filter.get()
}
});
}
});
// bind the value of the input to the underlying filter
Template.persons.events({
"input .age":function(event,template){
var currentValue=template.find(".age").valueAsNumber;
template.filter.set(currentValue);
}
});
标签:javascript,meteor,meteor-blaze,spacebars 来源: https://codeday.me/bug/20190628/1320340.html