如何使用 angular 1.6 更改输入文本的指令?
How to change the directive of an input text with angular 1.6?
目前我在app.js中有两个指令,用于跟随onkeypress验证,一个指令是验证only numerics[=23] =] 和另一个指令只验证 字母 [A-Z].
<input type="text" id="txt_siaf" class="form-control" ng-model="siaf" maxlength="10" valid-numeric />
鉴于我的输入文本当前分配了一个 valid-numeric 指令,我需要知道如何通过 valid-letters指令。
感谢您的支持。
好吧,我不认为您可以从输入中动态添加或删除指令,但您可以尝试其他解决方法,例如:
在控制器中:
$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
任何事件。
更新:
有一种方法可以动态添加或删除指令,但过程很长,请参阅:
https://www.codeproject.com/Tips/1038684/AngularJs-Add-Remove-Directives-Dynamically-Save-D
**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() {
});
目前我在app.js中有两个指令,用于跟随onkeypress验证,一个指令是验证only numerics[=23] =] 和另一个指令只验证 字母 [A-Z].
<input type="text" id="txt_siaf" class="form-control" ng-model="siaf" maxlength="10" valid-numeric />
鉴于我的输入文本当前分配了一个 valid-numeric 指令,我需要知道如何通过 valid-letters指令。
感谢您的支持。
好吧,我不认为您可以从输入中动态添加或删除指令,但您可以尝试其他解决方法,例如:
在控制器中:
$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
任何事件。
更新: 有一种方法可以动态添加或删除指令,但过程很长,请参阅:
https://www.codeproject.com/Tips/1038684/AngularJs-Add-Remove-Directives-Dynamically-Save-D
**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() {
});