I have a modal that has a controller "userCtrl". I call all the scripts in the page loading (end of the body), however, the modal, I load dynamically from the moment the user clicks the button, using the function $.get()
I load the modal and make a $(#modal).modal('show')
.
- Why can this occur?
- Is there any way to fix this?
- I wonder if this is because the controller script is loaded before the ng-controller element.
Function that loads the html of the modal where ng-controller
is included
showModal = function(id, path) {
$.get(path, response => {
$(".modals").append(response);
$(id).modal('show');
$(id).on("hidden.bs.modal", () => {
$(id).remove();
});
});
}
Modal strand
<div class="modal fade" id="mdCreateGrupos" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true" ng-controller="gruposCtrl" ng-init="init()">
Controller Excerpt
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've done that function that works for any modal.