Muppet
Muppet

Reputation: 386

Formatting Value in View Layer

Can someone please clarify this for me? How can I format a number in the view if the data is received from an API call (I can't format this on the back end).

For example, let score = '0.929304045';

In the template:

<div> {{ score }} </div>

If I add a computed function to do the formatting for me:

getScore(score) {
  return score.toFixed(2);
}

It gives me an error

_vm.getScore is not a function

If I call {{ getScore(score) }} in the view.

Any clarity on this will be greatly appreciated.

Upvotes: 1

Views: 46

Answers (1)

Bert
Bert

Reputation: 82439

Computed values are not methods. They act like properties. This is not a case where you would want to use a computed value. Instead, make getScore a method.

methods:{
  getScore(score){
    return score.toFixed(2)
  }
}

Alternatively, define a filter.

Upvotes: 3

Related Questions