What does the $compile function do and how can we use it to add a directive dynamically via js code?
Does anyone have fully functioning code samples?
What does the $compile function do and how can we use it to add a directive dynamically via js code?
Does anyone have fully functioning code samples?
Thought it will help a lot of developers out there to get something fully functioning on this. Here's a sample answer.
The $compile service compiles the fragment of html(<test text="n"></test>
) that includes the directive("test" as an element) and produces a function. This function can then be executed with a scope to get the "html output from a directive".
var compileFunction = $compile("<test text="n"></test>");
var HtmlOutputFromDirective = compileFunction($scope);
More details with full code samples here: http://www.learn-angularjs-apps-projects.com/AngularJs/dynamically-add-directives-in-angularjs