最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 2: String interpolation with function call - Stack Overflow

programmeradmin4浏览0评论

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 use string datatype not String – 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 and String? – 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
 |  Show 3 more ments

2 Answers 2

Reset to default 4

You 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>
发布评论

评论列表(0)

  1. 暂无评论