Calling method from a Angular 2 class inside template

hY8vVpf3tyR57Xib :

I have a angular 2 application that has a class called User. This user has a attribute called deleted_at that is either null or contains a datetime, obviously the user is deleted if the deleted_at property isn't null. This is how my user.ts file looks:

User.ts

export class User {
    id: number;
    email: string;
    created_at: string;
    first_name: string;
    last_name: string;
    deleted_at: any;

    name() {
        if (this.deleted_at === null) {
            return this.first_name;
        } else {
            return 'DELETED';
        }
    }
}

Now I expected that I could just call name in my template with a simple line:

{{ user.name }}

This however returns nothing, how can you call certain functions in the angular 2 template? Or isn't this allowed?

Edit: to clear stuff up a bit, this is a class User that I am using in my component user-list.component.ts, multiple users are handled in this component.

Thierry Templier :

Either you call the method like this:

{{user.name()}} // instead of {{user.name}}

For this approach you need to be aware that you will lose the execution context (this). See this question for more details:

Or you define your method as a getter so you can use user.name in your template:

get name() {
  if (this.deleted_at === null) {
    return this.first_name;
  } else {
    return 'DELETED';
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angular2 - infinite loop when i call method from a Angular 2 class inside template

From Dev

Issue with calling static template method inside a template class

From Dev

Calling a method from inside of another class

From Dev

calling static method from inside the class

From Dev

Multiple times method calling from angular template

From Dev

Calling virtual method of base template from derived variadic template class

From Dev

angular6 - infinite loop running when i call method from a Angular 6 class inside template

From Dev

Template method inside a class

From Dev

Calling a method from another method inside the same class using getattr

From Dev

Calling Attributes from Class inside of a Class Method, Code Error

From Dev

Calling method of template base class

From Dev

Calling a method from inside a thread to another class in python pyqt

From Dev

Calling a method inside Vue component from an outside class

From Dev

Calling a abstract method from a callback inside a abstract class

From Dev

Calling class method from inside conditionally rendered JSX

From Dev

Use of getter method or class variable inside angular6 template?

From Dev

Calling a method from a class

From Dev

Calling a template method on a class template parameter

From Dev

Calling derived template class method from base non-template class

From Java

Calling a method inside another method in same class

From Dev

Calling Model method inside template file

From Dev

Dependency Injection in Angular/ Calling a method that belongs to a service from a class

From Dev

Calling a static template method from a template function

From Dev

Calling a template function from a template function in a class

From Dev

Calling a method from the constructor in Angular2/IONIC2

From Dev

Calling protected ctor of inheriting class from within static template method of base class fails

From Dev

calling method in template class in template member function of another class

From Java

Calling super method from within an anonymous inner class inside the overridden method

From Dev

Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

Related Related

  1. 1

    angular2 - infinite loop when i call method from a Angular 2 class inside template

  2. 2

    Issue with calling static template method inside a template class

  3. 3

    Calling a method from inside of another class

  4. 4

    calling static method from inside the class

  5. 5

    Multiple times method calling from angular template

  6. 6

    Calling virtual method of base template from derived variadic template class

  7. 7

    angular6 - infinite loop running when i call method from a Angular 6 class inside template

  8. 8

    Template method inside a class

  9. 9

    Calling a method from another method inside the same class using getattr

  10. 10

    Calling Attributes from Class inside of a Class Method, Code Error

  11. 11

    Calling method of template base class

  12. 12

    Calling a method from inside a thread to another class in python pyqt

  13. 13

    Calling a method inside Vue component from an outside class

  14. 14

    Calling a abstract method from a callback inside a abstract class

  15. 15

    Calling class method from inside conditionally rendered JSX

  16. 16

    Use of getter method or class variable inside angular6 template?

  17. 17

    Calling a method from a class

  18. 18

    Calling a template method on a class template parameter

  19. 19

    Calling derived template class method from base non-template class

  20. 20

    Calling a method inside another method in same class

  21. 21

    Calling Model method inside template file

  22. 22

    Dependency Injection in Angular/ Calling a method that belongs to a service from a class

  23. 23

    Calling a static template method from a template function

  24. 24

    Calling a template function from a template function in a class

  25. 25

    Calling a method from the constructor in Angular2/IONIC2

  26. 26

    Calling protected ctor of inheriting class from within static template method of base class fails

  27. 27

    calling method in template class in template member function of another class

  28. 28

    Calling super method from within an anonymous inner class inside the overridden method

  29. 29

    Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java

HotTag

Archive