输入类型编号,即使字符串也听取改变

时间:2014-08-25 13:03:13

标签: angularjs

在我的角度应用程序中,我有以下内容:

<input  ng-name='{{quest.id}}'  type="number" class="form-control textInputBox inputMargin"
 ng-required='required' ng-model='$parent.input' >

和我的js:

scope.$watch('input', function(newVal, oldVal, scope) {
                    if (newVal !== oldVal) {
                        if (scope.input || scope.input === 0) {
                            scope.quest.ans.valid = true;
                            scope.quest.ans.optVal = scope.input;
                        } else {
                            scope.quest.ans.valid = false;
                        }
                    }
                }, true);

即使来自用户的输入不是num,我想要的是收听改变,是否可能? 尝试过更改,它也不起作用......

我输入的目的不是为了防止输入字符串而是警告它,我仍然想保持它的输入类型='数字'......

1 个答案:

答案 0 :(得分:1)

这只是一个建议。尝试使用模式输入文本:

<input ng-name='{{quest.id}}'  
       type="text" 
       class="form-control textInputBox inputMargin" 
       ng-required='required' 
       ng-model='$parent.input' >

然后你可能会在手表中:

scope.$watch('input', function(newVal, oldVal, scope) {
    if (newVal !== oldVal) {
        if ((scope.input || scope.input === 0) && !isNaN(newVal)) {
            scope.quest.ans.valid = true;
            scope.quest.ans.optVal = scope.input;
        } else {
            scope.quest.ans.valid = false;
        }
    }
}, true);