Last active
July 18, 2018 09:30
-
-
Save carmichaelize/fb2f5164fc9f2b89d1352e8bc65c1486 to your computer and use it in GitHub Desktop.
AngularJS $compile example
This file contains hidden or 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 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