0
I have a modal that has a "userCtrl" controller. I call all the scripts in the page loading (end of body), however, the modals, I load dynamically from the moment the user clicks the button, using the function $.get()
I carry the modal and do a $(#modal).modal('show')
.
The problem is that, the ng-controller that is contained in the modal that is loaded when the user clicks does not work, it is as if the controller does not work, does not execute anything that is inside that controller in the modal, as if it was not found.
- Why this can happen?
- Is there any way to fix this?
- I wonder if this is because the controller script is loaded before the element containing ng-controller.
Function that loads the html of the modal where the ng-controller
showModal = function(id, path) {
$.get(path, response => {
$(".modals").append(response);
$(id).modal('show');
$(id).on("hidden.bs.modal", () => {
$(id).remove();
});
});
}
Stretch of modal
<div class="modal fade" id="mdCreateGrupos" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" ng-controller="gruposCtrl" ng-init="init()">
Excerpt from the controller
app.controller("gruposCtrl", function($scope, $gruposService, $compile) {
$('#mdCreateGrupo').on('show.bs.modal', function (event) {
$(".alert").remove();
$("#textGrupo").val("");
});
$scope.init = function() {
console.log('inicio');
}
Everything was working correctly until I used the $.get function, before I was using ng-include, but it was very verbose because it was an ng-include for each modal, now I did this function that works for any modal.
There are several possibilities. Including a snippet of pertinent code can increase your chances of getting a reply that solves your problem.
– OnoSendai
I made some changes.
– Leonardo Theodoro