Here's an example which demonstrates the problem described in the title: https://plnkr.co/edit/Xn1qgYftc5YHMsrGj0sh?p=preview
Directive code:
.directive('translate', function($compile) {
return {
compile: function(element) {
var html = element.html().split('<br plural="">');
return function(scope, element, attrs) {
function c(h) {
element.html(h);
$compile(element.contents())(scope);
}
if (attrs.translate != '') {
scope.$watch(function() {
return scope.$eval(attrs.translate)
}, function(val, oldval) {
if (val == oldval && html[2] !== undefined) return;
var p = html[2];
html[2] = gettext(html[0], html[1], attrs.add !== undefined ? val + attrs.add : attrs.subtract !== undefined ? val - attrs.subtract : val);
if (p != html[2]) c(html[2]);
});
} else c(gettext(html[0]));
}
}
}
})
So the problem is when I toggle back directive to show with ng-if
- it probably doesn't get fully reseted with recompilation(?) and therefore it causes misbehavior.
How can I track when directive is inserted and removed from DOM? If there's a way then I could solve this with an indicator. But there must be some better way, right?