0
<div data-bind="foreach: ccOpenInvoiceListObservable">
  <div data-bind="foreach:Invoices">
        <span data-bind="text:$root.formatCurrency(AmountOutstanding)"></span>
  </div>
</div>

 <div data-bind="foreach: overDueBalancesObservable">
        <span data-bind="text: $parent.formatCurrency(OverDueAmount)"></span>  
 </div>

vm.formatCurrency = function (value) {
        return "$" + value().toFixed(2);
};

I am getting the error mentioned in the subject line when trying to call $parent.formatCurrency(OverDueAmount). For AmountOutstanding, the values are getting formatted.

user2585299
  • 873
  • 2
  • 19
  • 41
  • What does the formatCurrency() function look like? You need to post some more code, what you are showing doesn't appear to be the problem – PW Kad Sep 18 '13 at 14:59
  • I have added the formatCurrency method. – user2585299 Sep 18 '13 at 15:04
  • 1
    Is OverDueAmount an observable or a number? If the latter it would error when calling `value()` in the formatCurrency function – Eriedor Sep 18 '13 at 15:06

1 Answers1

1

You get the error because your OverDueAmount is not an observable property.

Use the ko.utils.unwrapObservable function in your formatCurrency to access the value (this handles both case when value is observable and when it is not see also When to use ko.utils.unwrapObservable?):

vm.formatCurrency = function (value) {
        return "$" + ko.utils.unwrapObservable(value).toFixed(2);
};

Or change your OverDueAmount to a ko.observable.

Community
  • 1
  • 1
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Thanks nemesv, that worked. But both AmountOutstanding & OverDueAmount are properties of ko.observable. Then why value() works for first & not for latter ? – user2585299 Sep 18 '13 at 15:13
  • According to the error `OverDueAmount` is a number and not a ko.observable... are you sure that OverDueAmount is a ko.observable? – nemesv Sep 18 '13 at 15:14