2015-12-03 34 views
-1

找到这些完美的解决方案在这里:Example禁用文本输入 - 角与jQuery

// Catch all events related to changes 
$('#textbox').on('change keyup', function() { 
    // Remove invalid characters 
    var sanitized = $(this).val().replace(/[^-.0-9]/g, ''); 
    // Remove non-leading minus signs 
    sanitized = sanitized.replace(/(.)-+/g, '$1'); 
    // Remove the first point if there is more than one 
    sanitized = sanitized.replace(/\.(?=.*\.)/g, ''); 
    // Update value 
    $(this).val(sanitized); 
}); 

HTML

<input type="text" id="textbox" /> 

,但我不能让一个控制器内这项工作。

怎么办?

+0

始终在SO上发布相关代码。 – Satpal

回答

0

一件事,你所能做的就是在你的输入使用NG-变化是这样的:

<div ng-controller="yourCtrl as ctrl"> 
    <input type="text" id="textbox" ng-change="ctrl.doSomething()" ng-model="ctrl.someVar"> 
</div> 
这样你将有机会获得您的控制器内部的输入,可以是这样的值

:为“被执行时输入改变由于与输入元件的用户交互”:

angular.module('yourModule').controller('yourCtrl',[function(){ 
    var self = this; 

    self.doSomething = function(){ 
     //you can access the value of the input using 
     // self.someVar 
    } 
}); 

的NG-变化(https://docs.angularjs.org/api/ng/input/input%5Btext%5D官方DOC)。


您还可以使用ngKeyup(官方文档:https://docs.angularjs.org/api/ng/directive/ngKeyup),那就是“在KEYUP评价”你怎么把它连接到一个功能是一样的NG-变化。你将只能选择其中的一个。