2015-12-03 36 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-變化。你將只能選擇其中的一個。