-
-
Save aravinthanpnk/fcf3789a1fb5e73a6ea8f80da7d0ff0c to your computer and use it in GitHub Desktop.
AngularJS $compile example
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 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