I'm loading a template and then compiling it against my scope using $compile
service.
var template = "<div> .. {{someCompilingStuff}} ..</div>";
var compiled = $compile(template)(cellScope);
and then using it in a popover
cellElement.popover({
html: true,
placement: "bottom",
trigger: "manual",
content: compiled
});
My template is quite complex and may take some time to compile.
How can I make sure that angular has finished compiling the template before using it in the popover ?
Edit: I tried to force angular to $apply()
before creating the popover, it does work but generate javascript errors which is not acceptable for me.
Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together.
Compile function: It is used for template DOM Manipulation and collect all of the directives. Link function: It is used for registering DOM listeners as well as instance DOM manipulation. It is executed once the template has been cloned.
AngularJS Directive's replace option can be used to replace the container element itself by directive content. By default, the directive content inserted as the child of the element directive is applied on. But using replace, that container element altogether can be replaced by directive's actual content HTML.
Pre-linking function Executed before the child elements are linked. Not safe to do DOM transformation since the compiler linking function will fail to locate the correct elements for linking. Post-linking function Executed after the child elements are linked.
$compile
allows you to use what is called a clone attach function which allows you to attach your elements to the document. Here is an example you may be able to use:
var template = "<div> .. {{someCompilingStuff}} ..</div>";
var compiled = $compile(template)(cellScope, function (clonedElement) {
cellElement.popover({
html: true,
placement: "bottom",
trigger: "manual",
content: clonedElement
});
});
Reference to Angular documentation about $compile usage
In these cases you probably would like to use $timeout
to fix the issue.
Something like this:
var template = "<div> .. {{someCompilingStuff}} ..</div>";
var compiled = $compile(template)(cellScope);
cellElement.popover({
html: true,
placement: "bottom",
trigger: "manual",
content: compiled
});
$timeout(function(){
cellElement.popover("show");
}, 0);
Here's an example JSFIDDLE I've created for you. Look how I show & hide the popover using $timeout
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With