2015-04-24 36 views

回答

0

$scope.CheckValue将拥有价值你的控制器。

请在提交console.log($scope.CheckValue),你会在console看到价值。

+0

Thanks.this是帮了我很多。 – Coder

+0

@Ervikas。如果它解决了您的问题,您可以接受答案。 – Zee

0

在控制器,你应该使用

$scope.CheckValue 

有时它是很好的定义,你在模板中使用同样的控制器属性。因此,您将对您的变量有更清晰的概述和控制。

0
function forceNumber(element) { 
    element 
    .data("oldValue", '') 
    .bind("paste", function(e) { 
     var validNumber = /^[-]?\d+(\.\d{1,2})?$/; 
     element.data('oldValue', element.val()) 
     setTimeout(function() { 
     if (!validNumber.test(element.val())) 
      element.val(element.data('oldValue')); 
     }, 0); 
    }); 
    element 
    .keypress(function(event) { 
     var text = $(this).val(); 
     if ((event.which != 46 || text.indexOf('.') != -1) && //if the keypress is not a . or there is already a decimal point 
     ((event.which < 48 || event.which > 57) && //and you try to enter something that isn't a number 
      (event.which != 45 || (element[0].selectionStart != 0 || text.indexOf('-') != -1)) && //and the keypress is not a -, or the cursor is not at the beginning, or there is already a - 
      (event.which != 0 && event.which != 8))) { //and the keypress is not a backspace or arrow key (in FF) 
     event.preventDefault(); //cancel the keypress 
     } 

     if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.')).length > 2) && //if there is a decimal point, and there are more than two digits after the decimal point 
     ((element[0].selectionStart - element[0].selectionEnd) == 0) && //and no part of the input is selected 
     (element[0].selectionStart >= element.val().length - 2) && //and the cursor is to the right of the decimal point 
     (event.which != 45 || (element[0].selectionStart != 0 || text.indexOf('-') != -1)) && //and the keypress is not a -, or the cursor is not at the beginning, or there is already a - 
     (event.which != 0 && event.which != 8)) { //and the keypress is not a backspace or arrow key (in FF) 
     event.preventDefault(); //cancel the keypress 
     } 
    }); 
} 

forceNumber($("#myInput")); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<input type="text" id="myInput" />