LazioTibijczyk
LazioTibijczyk

Reputation: 1937

Format formly form field

I want to format a field when the data finally updates the model. The number usually comes back with a decimal point so what I want to be able to do is format it to no decimal point number (parseNumber function does that).

e.g. vm.model.total = 34.54

I want to format this number to 34 on the fly.

I can't make it work...

vm.fields = [
    {
        className: 'row',
        fieldGroup: [
            {
                className: 'col-xs-12',
                key: 'total',
                type: 'input',
                    templateOptions: {
                    wrapperClass: 'row',
                    labelClass: 'col-xs-9 col-sm-3',
                    dataClass: 'col-xs-3 col-sm-2 col-sm-offset-right-7',
                    label: 'Total',
                    disabled: true
                },
                formatters: [parseNumber(vm.model.total, 0)]
             }
        ]
    }
];

Upvotes: 0

Views: 1366

Answers (1)

Pop-A-Stash
Pop-A-Stash

Reputation: 6652

Your example does not match the examples in the documentation

Your argument to formatters field is incorrect. That field is expecting a function, NOT THE RESULT of the function, which is what you have defined here.

You should either use an anonymous function or a named function:

formatters: [function(value){ return parseNumber(value, 0); }]

or

formatters: [removeDecimal]
//...

function removeDecimal(value) {
  return parseNumber(value, 0)
}

This is a working example from their own documentation which I have added a formatter to the first name field: https://jsbin.com/hapuyefico/1/edit?html,js,output

Upvotes: 1

Related Questions