Angular 2 templates methods vs getters

后端 未结 3 1751
天命终不由人
天命终不由人 2020-12-09 01:46

I\'m wondering if there is any benefit to do this:

  
{{getSomething()}}
export class MyComp { getSomething() { return ...} }
3条回答
  •  一个人的身影
    2020-12-09 02:22

    The difference is in the first case you use a function in expression, but in the second case it's not a function. So you can't use

    {{getSomething()}}
    export class MyComp { get getSomething() { return ...} }

    The benefits of using second method is to use encapsulation of the property inside the class and you need to access it outside the class.

    Getters and setters are part of ES5 spec. You can read about getter and setter.

提交回复
热议问题