2017-10-05 46 views
0

目前我有在app.js,这对于以下onkeypress事件验证两个指令,一个指令是验证仅NUMERICS和其他指令验证仅字母[A-Z]如何更改角度为1.6的输入文本的指令?

<input type="text" id="txt_siaf" class="form-control" ng-model="siaf" maxlength="10" valid-numeric /> 

鉴于我输入文本当前分配有效数字指令,我需要知道如何通过有效-字母指令来改变它。

我将不胜感激您的支持。

+0

你试图动态改变指令?感谢tushar –

回答

0

好吧,我不这么认为,你可以添加或动态地从输入指令删除,但你可以与其他变通玩喜欢:

$scope.validNumericDirective = true; 

和模板:

在控制器

<input type="text" id="txt_siaf" ng-if="validNumericDirective" class="form-control" ng-model="siaf" maxlength="10" valid-numeric /> 
<input type="text" id="txt_siaf" ng-if="!validNumericDirective" class="form-control" ng-model="siaf" maxlength="10" valid-letters /> 

这个逻辑将做的伎俩,但你必须在任何事件在你的控制器中切换validNumericDirective

更新: 有一种方法,您可以添加或动态删除指令,但它有一个漫长的过程中看到:

Angularjs dynamically adding and removing elements using directive

https://www.codeproject.com/Tips/1038684/AngularJs-Add-Remove-Directives-Dynamically-Save-D

2
**You change the value of variable $scope.showNumericDirective dynamically.** 

Add in template 
    <input type="text" ng-if="showNumericDirective" id="txt_siaf" class="form-control" ng-model="siaf" valid-numeric /> 
    <input type="text" ng-if="!showNumericDirective" id="txt_siaf" class="form-control" ng-model="siaf" valid-letters /> 

    var myapp = angular.module("myapp", []); 

    myapp.controller('controllerName', function($scope) 
    { 
    $scope.showNumericDirective = true; 
    }); 
    myapp.directive('validNumeric', function() { 

    }); 
    myapp.directive('validLetters', function() { 

    }); 
+0

,它正在为我工​​作 –