My current use case involves providing a dialog service where the content displayed differs based on the context. To achieve this, I manually compile a DOM element within the service method and utilize jQuery UI to display the dialog. Here is an example of the code:
var _view = jQuery('<div id="config-dialog"><span ng-include="\'' + $scope.configView + '\'" ng-controller="' + $scope.configController + '"></span></div>');
var _compiled = $compile(_view.contents())($scope);
Subsequently, I trigger a scope event that should be handled by a function defined in the controller
$scope.$broadcast('config-open', $scope.config);
Upon closing the dialog, I remove the "config-dialog" element from the DOM as shown below:
$(this).dialog("destroy");
jQuery('#config-dialog').remove();
However, upon reopening the dialog with a new controller instance, I observed that 'config-open' event gets triggered multiple times. It appears that the scope associated with the dynamically created ng-include is not properly destroyed. Despite my understanding that AngularJS scopes are linked to DOM elements and should be garbage collected when the element is removed, this does not seem to be happening in my scenario.
Hence, my question is whether AngularJS is expected to handle scope cleanup in my situation or if there is a more appropriate approach to implement my use case. Any insights on what I may be doing wrong would be greatly appreciated.