JSFiddle - React, Tailwind, and code Playground
by Nilesh Injulkar
HTML
<div ng-app='startApp'>
<div ng-controller='mycontroller'>
<form name='forms'>
{{surname}}
<br/>
Name :
<input name="myname" ng-model="thename" ng-maxlength="5"
required></input>
<div class="validationMessage"
ng-show="forms.myname.$dirty && forms.myname.$invalid">
<div ng-show="forms.myname.$error.required">
Message name is required.
</div>
<div ng-show="forms.myname.$error.maxlength">
Maximum 5 character length exceeded.
</div>
</div>
<br/>
Date :
<dateone nameatt='startDate'date='date'></dateone>
<div class="validationMessage"
ng-show="forms.startDate.$dirty && forms.startDate.$invalid">
<div ng-show="forms.startDate.$error.required">
Date is required.
</div>
</div>
{{forms}}
</form>
</div>
</div>
CSS
.validationMessage {
color: 'red';
}
JavaScript
var startAppModule = angular.module('startApp',[]);
startAppModule.controller('mycontroller', function($scope){
$scope.surname = "Injulkar";
$scope.thename = "Nilesh";
$scope.date = '25/10/2012';
});
startAppModule.directive("dynamicName",function($compile){
return {
restrict:"A",
terminal:true,
priority:1000,
link:function(scope,element,attrs){
console.log(attrs.dynamicName);
console.log("-", scope);
var name = scope.$eval(attrs.dynamicName);
console.log("--", name);
if (name) {
element.attr('name', name);
element.removeAttr("dynamic-name");
$compile(element)(scope);
}
}
};
});
startAppModule.directive('validdate', function(){
return {
restrict: 'A',
link : function(scope, elem, attrs){
console.log('date' scope.date, scope.dt);
}
};
}
startAppModule.directive('dateone', function($compile){
return {
restrict: 'E',
scope: {
date: '='
},
template: "<input type='text' ng-model='dt' \
dynamic-name='dateinputname' required validdate>",
link : function(scope, elem, attrs){
scope.dateinputname = attrs.nameatt;
scope.format = 'MM/dd/yyyy';
scope.dt = scope.date;
$compile(elem.contents())(scope);
scope.$watch('dt', function () {
scope.date = scope.dt;
});
}
};
});