Skip to content

Instantly share code, notes, and snippets.

/gist:9bd089e2ca33d0c7d052 Secret
Created Dec 24, 2014

Embed
What would you like to do?
app.directive("formbuilder", function($http, $compile) {
var getTemplateUrl = function(field) {
var type = field.elementType;
var templateUrl = '';
switch(type) {
case 'textfield':
templateUrl = './views/directive-templates/field/textfield.html';
break;
case 'email':
templateUrl = './views/directive-templates/field/email.html';
break;
case 'textarea':
templateUrl = './views/directive-templates/field/textarea.html';
break;
case 'checkbox':
templateUrl = './views/directive-templates/field/checkbox.html';
break;
case 'date':
templateUrl = './views/directive-templates/field/date.html';
break;
case 'dropdown':
templateUrl = './views/directive-templates/field/dropdown.html';
break;
case 'hidden':
templateUrl = './views/directive-templates/field/hidden.html';
break;
case 'password':
templateUrl = './views/directive-templates/field/password.html';
break;
case 'radio':
templateUrl = './views/directive-templates/field/radio.html';
break;
}
return templateUrl;
}
var linker = function(scope, element) {
// GET template content from path
console.log(scope);
var templateUrl = getTemplateUrl(scope.field);
$http.get(templateUrl).success(function(data) {
element.html(data);
$compile(element.contents())(scope);
});
}
return {
template: '<div>{{field}}</div>',
restrict: "E",
scope: {
elementType: "="
},
link: linker
}
});
<div ng-repeat="formElement in formElements">
<form>
<formbuilder elementType="formElement.elementType"></formbuilder>
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.