I am developing my first Angular app and having trouble with string interpolation.
I have a ponent 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?
I am developing my first Angular app and having trouble with string interpolation.
I have a ponent 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?
Share Improve this question edited Feb 7, 2020 at 14:52 Jacques 7,1358 gold badges46 silver badges106 bronze badges asked Feb 22, 2017 at 13:58 LucaLuca 631 gold badge2 silver badges8 bronze badges 8-
1
How
action
looks like? I usually usestring
datatype notString
– Avinash Raj 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 e from your ponent ts file or from a local variable defined with the # tag. – Maxime Gélinas Commented Feb 22, 2017 at 14:07 -
@MaximeGélinas i now have
get text() { return "Test";}
and{{action.text}}
in my ponent. But there is now no text shown (at least it doesn't crash anymore). The variable action is defined in the associated Component file – Luca Commented Feb 22, 2017 at 14:18 -
@AvinashRaj this class only holds a few variables and this method. What's the difference between
string
andString
? – Luca 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()}}
– Luca Commented Feb 22, 2017 at 14:25
2 Answers
Reset to default 4You could convert it to a property:
get text(): string { return "Test"; }
Your template bees:
<span>{{ation.text}}</span>
You refer to action.title
, but ation.getText()
. Certainly a typo?
<span>{{action.getText()}}</span>