问题描述
指令中的ngModel控制器是否可以选择在视图中显示与ngModel值不同的内容,或者我必须为输入编写自定义html指令?
我写了一个逗号分隔符指令.一切都很好,但是ngModel的值由,
从Number更改为String.
我的意思是我可以输入值1000000
但它在视图中显示1,000,000
吗?
Does ngModel controller in directive have any option to show something different from ngModel value in view, or i have to write custom html directive for input?
I wrote a comma-separator directive. everything is fine, but ngModel value change from Number to String with ,
.
I mean can i have input with value 1000000
but it shows 1,000,000
in view?
更新:
在输入框中显示1,000,000
,但其值为1000000
.
Update:
In the input box shows 1,000,000
, but its value be 1000000
.
更新2
我了解$formatters
和$parsers
.但是问题是$formatters
不会损害实际值,但是它可以在ngModel直接更改时起作用,而不是通过输入元素来输入,并且$parsers
监视所有更改,但也会更改实际值.
Update 2
I know about $formatters
and $parsers
. but the problem is $formatters
doesn't harm real value, but it works when ngModel changed directly, not by typing in input element, and $parsers
watch all changes, but change real value too.
推荐答案
使用过滤器来实现此目的
use filter to achieve this
HTML
<div ng-controller="demoController">
<input type="text" value="{{val | number}}">
</div>
控制器:
var app = angular.module('demoApp',[]);
var app = angular.module('demoApp', []);
app.controller('demoController', ['$scope', '$http', function($scope, $http, $cookies, $location){
$scope.val = 1000000;
}]);
这篇关于Angular ngModel的不同观点和价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!