5

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?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
  • 1
    from a object oriented perspective, the model should have private privileges to access and alter its own properties.. pipes are more for transforming a value for display.. Say for example you want to capitalize Fullname then you would access the property using the model methods and the pipe will transform its value to display it in a different way – Mehdi Dec 16 '17 at 00:28
  • 1
    Isn't it expensive in terms of the number of times the method is called ? – Bilel Noômene Dec 16 '17 at 00:39

1 Answers1

5

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.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565