Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Pipes vs methods in model

I use functions in models as in this example:

//user.madel.ts
class User {
    getFullname () {
        return this.firstname + '  ' + this.lastName;
    }
}

// in html I can do this:
<span> {{ user.getFullName() }} <span>

Is it proper or should I use pipes?

like image 745
Bilel Noômene Avatar asked Dec 16 '17 00:12

Bilel Noômene


1 Answers

Angular pipes work best with a single value, because pure pipes have performance advantages. Since both firstname and lastname are expected to be changed, pure pipe isn't an option, and it will end as either

{{ user.firstname | fullname(user.lastname }}

or

{{ user | fullname }}

impure pipe that has no performance advantages over getter method.

If calculations are inexpensive, it can be either getter method or get property accessor:

get fullname () {return this.firstname + '  ' + this.lastname;}

Otherwise returned value should be cached for performance reasons.

like image 162
Estus Flask Avatar answered Oct 13 '22 02:10

Estus Flask