简体   繁体   English

HTML5输入使用Angular空值0

[英]Html5 input using Angular empty value 0

If I have an input such as below. 如果我有如下输入。 What is the cleanest way I can ensure the empty string (input) is always 0.00. 我可以确保空字符串(输入)始终为0.00的最干净方法是什么。

I was thinking it could be done with a watch but was wondering if there would be an easier way built into angular or html5 to handle it. 我当时以为可以用手表来做,但是想知道是否会有更简单的方法内置在angular或html5中来处理它。

<input class="form-control" type="number" step="0.01" placeholder="$" 
                                           data-ng-model="status.approved.price" />

Edit: What I actually needed was blur which handles clicking out of input as well as tabbing out. 编辑:我真正需要的是模糊处理,可以处理输入的点击和跳出。 Tomek Sułkowski helped me get to this solution as you can see in this directive. 如您在本指令中所见,TomekSułkowski帮助我获得了此解决方案。 Once the user leaves the input it will default to 0 if there is no value. 用户离开输入后,如果没有值,则默认为0。

return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, elem, attrs, ngModelCtrl) {
        elem.on('blur', function() {
            if (!elem.val()) {
                elem.val('0');
                ngModelCtrl.$setViewValue('0');
            }
        });
    }
};

Actually a better solution than setting a watch in a controller would be to create a directive that sets proper modelValue if viewValue is " " (space). 实际上,比在控制器中设置监视更好的解决方案是创建一个指令, 如果viewValue为“” (空格), 该指令将设置适当的modelValue It will be far more reusable, because if you'll have to use it in other places you won't need to write the same logic in each of those cases. 它将具有更高的可重用性,因为如果您不得不在其他地方使用它,则在每种情况下都无需编写相同的逻辑。

Mind that Angular trims input values by default . 注意默认情况下Angular会修剪输入值 You need to turn it off on your input (hence the ng-trim="false" ). 您需要在输入中将其关闭(因此ng-trim="false" )。

Something like this: 像这样:

 angular.module('app', []) .directive('myZeroable', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, elem, attrs, ngModelCtrl) { ngModelCtrl.$parsers.push(function (viewValue) { if (viewValue === ' ') { return 0; // or '0.00' - depending on your requirements } return viewValue; }); } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script> <div ng-app="app"> <label>Write a single space</label> <input ng-model="test" my-zeroable ng-trim="false" /> <pre>Current model value is: {{ test | json }}</pre> </div> 

The Jade markup: 翡翠标记:

main(ng-app="demo")
  input(type="text", ng-model="test", ng-controller="demo-controller" ng-change="setInitVal(test)") 
  {{ test }}

The angular code: 角度代码:

    "use strict";
    var demo = angular.module('demo',[]);

    demo.controller('demo-controller', function($scope){
      $scope.setInitVal = function(inputVal){

        if (!inputVal) {
            $scope.test = '0.00';
        }
      };
    });

Heres the demo: http://codepen.io/nicholasabrams/pen/EjbLpW 这是演示: http : //codepen.io/nicholasabrams/pen/EjbLpW

This would work, {{ test != ""? test: '0.00' }} 这会起作用, {{ test != ""? test: '0.00' }} {{ test != ""? test: '0.00' }}

your updated jade mark up would be, 您更新的翡翠标记是

main(ng-app="demo")
  input(type="text", ng-model="test", ng-controller="demo-controller" ng-change="setInitVal(test)") 
  {{ test != ""? test: '0.00' }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM