我在模型中使用函数,如下例所示:
//user.madel.ts
class User {
getFullname () {
return this.firstname + ' ' + this.lastName;
}
}
// in html I can do this:
<span> {{ user.getFullName() }} <span>
它是否合适或我应该使用管道?
角管最适合单一值,因为纯管具有性能优势。由于预计firstname
和lastname
都会被改变,纯管道不是一种选择,它将以任何一种方式结束
{{ user.firstname | fullname(user.lastname }}
要么
{{ user | fullname }}
与吸气剂方法相比没有性能优势的不纯管道。
如果计算成本低廉,它可以是getter方法或get
属性访问器:
get fullname () {return this.firstname + ' ' + this.lastname;}
否则,出于性能原因,应缓存返回值。