回答

2

Hackish?方式,$看NG-模型在你的控制器:

<input type="text" ng-model="myText"> 

控制器:

$scope.$watch('myText', function() { 
    // put logic to strip out all non-character characters here 
    if ($scope.myText ... regex to look for ...) { 
     // strip out the non-characters 
    } 
}) 

的最佳方式,在指令使用$解析器。 我不会重复@ pkozlowski.opensource提供的已经很好的答案,所以这里是链接:https://stackoverflow.com/a/14425022/215945

不要尝试使用ng-change - 它会导致问题。见AngularJS - reset of $scope.value doesn't change value in template (random behavior)

更新: ng-pattern也可用于定义一个正则表达式,这将限制什么是允许的领域。另见"cookbook" page about forms

+0

看来你不明白我的问题。我希望该用户在文本框中输入除字符以外的任何值。所以我认为在这里我们必须使用onkeypress函数。你说什么? – user2013463

+1

我相信我提供的两种解决方案应该可以工作。我只是用关于ng-pattern的信息更新了我的答案。如果您无法使用以上三种解决方案中的任何一种解决问题,请发布[小提琴](http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/)或[plnkr](http://plnkr.co/edit/)你尝试过。 –

+0

我已经使用了ng-pattern用于这个目的,它为我提供了很好的验证目的,但实际上现在我想让用户甚至不能在文本框中输入任何字符以外的东西。你能帮我解决吗? – user2013463

相关问题