Last active
August 29, 2015 14:01
-
-
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
This file contains 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
(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