HTML 输入 - AngularJS - 显示不超过 3 位数字



当我在输入框中输入 4 位数字(不包括小数)或更多数字时,当我单击(模糊)时,它突然从数字输入框中消失。会不会是货币过滤器在做这件事?

如果我将其记录到控制台,则模型仍然保留该值,它只是从视图中消失了。

<div class="input-group col-sm-2">
    <div class="input-group-addon">$</div>
    <input type="number" step="any" min="0" class="form-control" id="promoSetupFee" data-ng-model="fees.setup.promo" data-ng-format-curr data-ng-blur="updateSetupTotal()">
</div>

crtPromoDir.directive('ngFormatCurr', ['$timeout', '$filter', function($timeout, $filter)
{
    return {
        link: function(scope, element, attrs)
        {
            $timeout(function()
            {
                element.val($filter('currency')((element.val() || 0), '', 2));
            });
            element.blur(function()
            {
                element.val($filter('currency')((element.val() || 0), '', 2));
            });
        }
    };
}]);

编辑:似乎类型编号的HTML输入不接受逗号,有什么方法可以从我拥有的指令中删除逗号吗?

编辑:

$scope.updateSetupTotal = function()
{
    $scope.fees.setup.total = (parseFloat($scope.fees.setup.promo) || 0.00)+(parseFloat($scope.fees.setup.loc) || 0.00)+(parseFloat($scope.fees.setup.premium) || 0.00)+(parseFloat($scope.fees.setup.content) || 0.00);
    $scope.updatePromoTotal();
};

编辑:快速解决方法是使用类型文本作为输入而不是数字,但这意味着模型现在将是字符串而不是浮点数。

要从字符串中删除逗号,请执行以下操作:

element.val($filter('currency')((element.val().replace(',','') || 0), '', 2));

我通过使用亚历克斯答案的变体来让它工作。基本上,由于currency过滤器添加了逗号,因此我必须在应用该过滤器后和发送到输入框之前立即删除它们。

crtPromoDir.directive('ngFormatCurr', ['$timeout', '$filter', function($timeout, $filter)
{
    return {
        link: function(scope, element, attrs)
        {
            $timeout(function()
            {
                element.val($filter('currency')((element.val() || 0.00), '', 2).replace(',',''));
            });
            element.blur(function()
            {
                element.val($filter('currency')((element.val() || 0.00), '', 2).replace(',',''));
            });
        }
    };
}]);

这允许我将模型保留为 int 并避免使用字符串值。

最新更新