3

I am developing my first Angular app and having trouble with string interpolation.

I have a component which contains the following element:

`<span>{{action.getText()}}</span>`

action is of Type Action which has the following method:

getText(): String { return "Test"; }

The variable action is defined correctly since I can access properties via {{}} without a problem. e.g. {{action.title}}

So my question is, is this even possible to access this function and if yes, what am I doing wrong?

8
  • 1
    How action looks like? I usually use string datatype not String Commented Feb 22, 2017 at 14:00
  • Try with a propery getter get text() { return "Test"; } and {{action.text}}. Work only if the action object come from your component ts file or from a local variable defined with the # tag. Commented Feb 22, 2017 at 14:07
  • @MaximeGélinas i now have get text() { return "Test";} and {{action.text}} in my component. But there is now no text shown (at least it doesn't crash anymore). The variable action is defined in the associated Component file Commented Feb 22, 2017 at 14:18
  • @AvinashRaj this class only holds a few variables and this method. What's the difference between string and String? Commented Feb 22, 2017 at 14:19
  • 1
    And for general understanding: So, it's not possible to call a function this way? Let's say, my class Action has a date property and i want to display the minutes value. Then i cannot write {{action.date.getMinutes()}} Commented Feb 22, 2017 at 14:25

2 Answers 2

4

You could convert it to a property:

get text(): string { return "Test"; }

Your template becomes:

<span>{{ation.text}}</span>
Sign up to request clarification or add additional context in comments.

9 Comments

Scratch that. You can convert to a property, but binding to a function result actually works as expected.
Now, it shows nothing at all where the <span> should be
Anything in the console?
So, in your comment you're saying that action.getText() should actually work fine?
With the function call action.getText() the application crashes. With the getter, there's nothing in the console
|
0

You refer to action.title, but ation.getText(). Certainly a typo?

<span>{{action.getText()}}</span>

1 Comment

ation is a typo, which I have now corrected. The reference to action.title should just show, that the variable is defined correctly, since i can access this property.

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.