Green Computers
Green Computers

Reputation: 753

Two way data binding in angular 2 is not happening when i use function to bind data on textbox

I am trying to do two-way data binding using a function. Below is my code.

<input type="text" id="txtCurrentPrice" class="traderviewTxtBox" [(value)]="numberFormat(23236448)">

my function is

numberFormat(x) {
            return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    } 

I am getting the below error. Can anybody help.

Unhandled Promise rejection: Template parse errors:
Parser Error: Unexpected token '=' at column 23 in [numberFormat(23236448)=$event] in myComponent@241:101 ("<input type="text" id="txtCurrentPrice" class="traderviewTxtBox" [ERROR ->][(value)]="numberFormat(23236448)">
                                <!--<dx-text-box id="txtCurrentP"): myComponent@241:101

Upvotes: 0

Views: 526

Answers (1)

benPearce
benPearce

Reputation: 38353

Two-way databinding doesn't seem to be possible to a function as the function is a output operation.

You can bind to the value property one-way using the function by removing the parentheses:

<input type="text" id="txtCurrentPrice" class="traderviewTxtBox" [value]="numberFormat(23236448)">

Upvotes: 0

Related Questions