Skip to content

Instantly share code, notes, and snippets.

@jacoor
Last active August 29, 2015 14:06
Show Gist options
  • Save jacoor/7e0048686a96e440818a to your computer and use it in GitHub Desktop.
Save jacoor/7e0048686a96e440818a to your computer and use it in GitHub Desktop.
angular simple aa-field directive for generating form fields from django options response
<li class="form-field" ng-class="{invalid: errors[name], error: errors[name]}">
<input ng-model="model" ng-init="model" type="{[{field.type}]}" ng-required="{[{field.required}]}" placeholder="{[{field.label}]}" ng-maxlength="{[{field.max_length}]}" name="{[{name}]}"/>
<ng-include ng-if="errors[name]" src="'templates/directives/aa-forms/error_list.html'"></ng-include>
</li>
/* jslint browser: true, undef: true, newcap: true, forin: true, sub: true, white: true, indent: 4, unused: false */
/* globals define: true */
define([
'app/app'
], function(
App
) {
"use strict";
var AaInput = App.directive('aaInput', function($compile, $http) {
var renderTemplate = function(template, $scope, $element){
var promise = $http.get('templates/directives/aa-forms/'+template+'.html');
promise.success(function(html){
$element.html(html).show();
$compile($element.contents())($scope);
});
};
return {
restrict: 'AE',
replace: true,
scope: {
'model': '=aaInputModel',
'name' : '@aaInputName',
'field': '=aaInputField',
},
link: function($scope, $element, $attrs) {
if (['string'].indexOf($scope.field.type)!=-1){
$scope.field.type = 'text';
$scope.fieldTemplate = 'input';
}
renderTemplate($scope.fieldTemplate, $scope, $element);
},
};
});
return AaInput;
});
<ul class="errorlist">
<li ng-repeat="e in errors[name]" ng-bind-html="e"></li>
</ul>
<aa-input aa-input-model="user.first_name" aa-input-name="first_name" aa-input-field="accountFormFields.first_name"></aa-input>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment