I have a directive that is shown under some condition using ng-if.
Once the condition is met, the link function triggers and I use $compile to replace the directive content with some HTML blocks.
Then at some point the directive is hidden and when it is shown again, the same code is called to recreate the HTML blocks. I have tried setting a flag on the scope, but once ng-if is set to false, the scope values are lost.
What would be the correct approach to avoid recreating the HTML blocks each time ng-if set back to true ? In other words, how can I make sure the HTML blocks are only generated the first time ng-if is set to true ?
link: function (scope, element, attrs) {
if (!scope.initialized) {
$http.get('some url to method on server' } }).then(function (response) {
var list = '';
response.data.forEach(function (link) {
list += '<a href=' + link + '>' + link + '</a>';
});
element.html('<div>' + list + '</div>');
$compile(element.contents())(scope);
scope.initialized = true;
}, function (response) {
});
}