Last active
August 29, 2015 14:06
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="errorlist"> | |
<li ng-repeat="e in errors[name]" ng-bind-html="e"></li> | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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