Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait till the end of $compile in angularJS

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.

like image 643
kyori Avatar asked Jul 23 '15 13:07

kyori


People also ask

What does $compile do in AngularJS?

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.

What is compile and link in AngularJS?

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.

What is replace in AngularJS directive?

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.

What is compile pre and post linking AngularJS?

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.


2 Answers

$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

like image 53
Zachary Kuhn Avatar answered Oct 05 '22 19:10

Zachary Kuhn


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.

like image 34
Amir Popovich Avatar answered Oct 05 '22 19:10

Amir Popovich