我有一个自定义的数据类型Message
:如何在AngularJS中使用方法作为过滤器?
function Message(body, author, date) {
this.body = body;
this.author = author;
this.date = date;
this.stars = [];
}
Message.prototype.hasStars = function() {
return this.stars.length !== 0;
};
我也做了重复这些消息的阵列上:
<li ng-repeat='message in messages | orderBy:"-stars.length"'>…</li>
我怎样才能过滤器添加到它调用message.hasStars()
?我尝试以下,但他们都没有效果:
message in messages | filter:message.hasStars() | orderBy:"-stars.length"
message in messages | filter:message:hasStars() | orderBy:"-stars.length"
message in messages | filter:message.hasStars | orderBy:"-stars.length"
message in messages | filter:message:hasStars | orderBy:"-stars.length"
你需要为 – zsong
自定义过滤器@sza这是不正确的。看到“Jared Words”的答案。 – jessegavin
@jessegavin这不过是一个过滤器。你可以使用'filter'模块或者只是一个函数来创建一个过滤器。 – zsong