I created and directive in angularjs app which produces styled input in my application. It looks like this:
AC.directive('formInput',function ($compile) {
return {
transclude: true,
replace: true,
scope:{},
templateUrl: '/views/partials/form/input.html',
restrict: 'E',
link: function(scope, element, attrs){
scope.opts = attrs;
if(attrs.ngModel){
element.find('input').attr('ng-model', attrs.ngModel);
$compile(element.contents())(scope.$parent);
}
if(!attrs.type){
scope.opts.type = 'text';
}
}
};
}
)
And the template for it is:
<label class="acxm-textfield {{opts.cssclass}}">
<span ng-bind="opts.labeltext"></span>
<input type="{{opts.type}}" name="{{opts.inputname}}" value="{{opts.inputvalue}}" placeholder="{{opts.placeholder}}" ng-maxlength="{{opts.maxLength}}"/>
</label>
The call is simple:
<form-input ng-model="UserProfile.FirstName" max-length="50" labeltext="{{'GENERAL.name' | translate}}" cssclass="acxm-p-horizontal" inputname="name" inputvalue="{{UserProfile.FirstName}}"></form-input>
I wanted to create validation for this field and I added an error information:
<span ng-show="showError(userInfoForm.name,'email')">
You must enter a valid email
</span>
And showError is:
$scope.showError = function(ngModelController, error) {
return ngModelController.$error[error];
};
Basically it is copied form book: Mastering Web Application Development with AngularJS. And I have problem because when i log my form, which name is userInfoForm, in console I got {{opts.inputname}} instead of name property which value here should be "name". And the question is what am I doing wrong?
name="{{opts.inputname}}"
withdynamic-name="opts.inputname"
– Khanh TO Jan 30 '14 at 15:49required
to my input field. Silly me. I posted as an answer. Please have a look. – Khanh TO Feb 4 '14 at 12:08