Calling method from a Angular 2 class inside template

前端 未结 2 752
野性不改
野性不改 2020-12-11 15:09

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 de

2条回答
  •  青春惊慌失措
    2020-12-11 15:45

    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:

    • ng-lightning - data object is undefined on lookup

    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';
      }
    }
    

提交回复
热议问题