Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active July 18, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carmichaelize/fb2f5164fc9f2b89d1352e8bc65c1486 to your computer and use it in GitHub Desktop.
Save carmichaelize/fb2f5164fc9f2b89d1352e8bc65c1486 to your computer and use it in GitHub Desktop.
AngularJS $compile example
function MyDirective ($templateRequest, $compile) {
var templateBase = '/directives/my_directive/views/';
var childTemplates = {
child1: 'child1.html',
child2: 'child2.html'
};
return {
restrict: 'E',
scope: {
child: '='
value: '='
},
compile: function (tElement, tAttrs) {
return function (scope, iElement, iAttrs) {
//Build template url
var templateUrl = templateBase + childTemplates[scope.child];
//Render template
$templateRequest(templateUrl, true).then(function(template) {
// Set the template as the content of the element.
iElement.html(template);
// Compile it amd link it to the isolated scope.
$compile(iElement.contents())(scope);
});
};
}
};
}
angular.module('MyApp')
.directive('MyDirective', MyDirective);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment