Skip to content

Instantly share code, notes, and snippets.

@wildermuthn
Created April 22, 2014 22:20
Show Gist options
  • Save wildermuthn/11196401 to your computer and use it in GitHub Desktop.
Save wildermuthn/11196401 to your computer and use it in GitHub Desktop.
for morgan formItem directive
'use strict';
angular.module('crossroadsDonateApp')
.directive('formItem', ['$compile', '$http', '$templateCache', function($compile, $http, $templateCache, $rootScope) {
var rootScope = $rootScope;
var getTemplate = function(contentType) {
var templateLoader,
baseUrl = 'views/',
templateMap = {
text: 'form-item-text.html',
radios: 'form-item-radios.html',
select: 'form-item-select.html',
};
var templateUrl = baseUrl + templateMap[contentType];
templateLoader = $http.get(templateUrl, {cache: $templateCache});
return templateLoader;
};
return {
restrict: 'A',
scope: {
item: '=',
change: '&',
},
link: function postLink(scope, element, attrs) {
scope.valid = "";
scope.$on('form.validate', function() {
if (scope.item.required != false && (scope.item.inputValue == '' || scope.item.inputValue == undefined)) {
scope.valid = "has-error";
}
else {
scope.valid = "";
}
});
scope.$watch('item.inputValue', function(newValue) {
scope.change({});
});
var loader = getTemplate(scope.item.type);
var promise = loader.success(function(html) {
element.html(html);
}).then(function (response) {
scope.selected = {};
if (scope.item.type == 'radios' || scope.item.type == 'select') {
for (var i in scope.item.values) {
var value = scope.item.values[i];
if (value == scope.item.placeholder) {
scope.selected[value] = true;
scope.item.inputValue = value;
}
else {
scope.selected[value] = false;
}
}
}
$compile(element.contents())(scope);
});
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment