2

I have created a simple service (MyService) which has a static public function called hello which returns the string "hello"

In another component I import the MyService and want to place the hello output in an expression in a template like so:

{{ myService.hello() }}

I was able to make this work with a non-static method (using the instance injected in the component constructor):

{{ myInstance.hello() }}

but I can't seem to do the equivalent using the static method. The browser complains that hello is not a function. Why not?

1 Answer 1

2

Static methods can't be used in the template directly.
The scope of the template is the component instance and in bindings only identifiers from this scope can be referenced.

export class MyComponent {
  hello = MyService.hello
}

allows

{{hello()}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.