I want to create a custom directive that displays as an input type element. The directive should reuse the structure of corner validation. Below is the directive custom-inputin the action I created:
<!doctype html>
<html ng-app="validationApp">
<body>
<div class="container" ng-controller="ValidationController as validationController">
<form name="myForm">
{{employee | json}}
<custom-input ng-required="true" ng-model="employee.name" name="employee.name" id="employeeName" ng-pattern="/^[0-9]{1,7}$/"/></custom-input>
<span ng-show="myForm['employee.name'].$error.required">This is a required field</span>
<span ng-show="myForm['employee.name'].$error.pattern">This is a invalid field</span>
</form>
</div>
<script type="text/ng-template" id="/templates/customInput.html">
<div>
<input type="text" name="{{name}}" ng-model="newInput" id="{{id}}">
</div>
</script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.js"></script>
</body>
</html>
The corresponding javascript is as follows:
angular.module('validationApp', [])
.controller("ValidationController", function(){
})
.directive("customInput", function(){
return {
restrict: "E",
require : "ngModel",
replace: "true",
templateUrl : "/templates/customInput.html",
scope : {
id : "@",
name : "@"
},
link: function(scope, element, attrs, ngModelCtrl){
scope.$watch('newInput', function(newValue){
ngModelCtrl.$setViewValue(newValue);
});
ngModelCtrl.$render = function(){
var viewValue = ngModelCtrl.$viewValue;
if(viewValue){
scope.newInput = viewValue;
}
}
}
}
});
I try to apply the test ng-requiredand ng-patternfor that user input. I ran into two problems:
- In angularjs 1.2.6, I can run the check
ng-requiredin custom-input, but in 1.3.0, the check does not start. - I can not run the check
ng-patternin both versions.
, $setViewValue of ngModelController . , - , SSN.
plunker 1.2.6 1.3.0 :
Angularjs 1.2.6
Angularjs 1.3.0