Skip to content

Instantly share code, notes, and snippets.

@thgreasi
Last active August 29, 2015 14:01
Show Gist options
  • Save thgreasi/7152499c0e91973c4820 to your computer and use it in GitHub Desktop.
Save thgreasi/7152499c0e91973c4820 to your computer and use it in GitHub Desktop.
A simple directive to dynamically load proper directive based on the type of the provided model
(function() {
"use strict";
angular.module('gen.genericDirectives', [])
.directive('genDynamicDirective', ['$compile',
function($compile) {
return {
restrict: "E",
require: '^ngModel',
scope: true,
link: function(scope, element, attrs, ngModel) {
var ngModelItem = scope.$eval(attrs.ngModel);
scope.ngModelItem = ngModelItem;
var getView = scope.$eval(attrs.genGetDynamicView);
if (getView && typeof getView === 'function') {
var templateUrl = getView(ngModelItem);
if (templateUrl) {
element.html('<div ng-include src="\'' + templateUrl + '\'"></div>');
}
$compile(element.contents())(scope);
}
}
};
}
]);
})();
// function getView (ngModelItem) {
// var template = '';
// switch (ngModelItem.Type) {
// case 'Type1':
// template = 'Type1.html';
// break;
// case 'Type2':
// template = 'Type2.html';
// break;
// }
// return template;
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment