Alliswell
Alliswell

Reputation: 1583

How to apply filter number to input value | Angularjs

I need to apply angular's filter number to input value directly.

I thought the code below must work, but it doesn't..

<input type="text" ng-model="product.price" value="{{product.price | number}}" required>

Is it possible to implement this by default Angular resources? Without writting directives?

Upvotes: 1

Views: 4213

Answers (2)

Ved
Ved

Reputation: 12093

This can be achived using regex or ng-pattern.

<input type="text" ng-model="product.price" ng-pattern="/^(\d)+$/" required>

2.If you want to format the data value, you can do something like this.

$scope.product.price= parseInt($scope.product.price)

Upvotes: 2

Brent Washburne
Brent Washburne

Reputation: 13148

You don't need to use the value attribute when you have the ng-model attribute:

<input type="text" ng-model="product.price" required>

Set the value of product.price in the controller. If you want to format the price, do it in the controller as well.

Upvotes: 1

Related Questions