Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unbootstrap after angular.bootstrap has been initiated? [closed]

Tags:

angularjs

Having trouble trying to recompile angular app.

Is there a way to unbootstrap after calling angular.bootstrap?

Once I do angular.bootstrap when already bootstrapped, causes errors

Thanks!

like image 617
Mohamed El Mahallawy Avatar asked Mar 18 '23 21:03

Mohamed El Mahallawy


1 Answers

It does not seem like angular was designed to do this, and I can't think of any good reason why you would want to (but maybe you do). That said, from looking at the angular.js source code it seems like the only way that this could work would be to remove the element you originally bootstrapped from the DOM and then re-add it and then try bootstraping again.

Here's a jsfiddle proof of concept that bootstraps a super simple app and then after 5 seconds bootstraps itself again. I'd be extremely hesitant to do something like this on a complex app but it does seem to work.

 var bootstrapApp = function(appDiv) {
    var isSecondTime = false;
    if (appDiv) {
        document.body.removeChild(appDiv);
        isSecondTime = true;
    }
    appDiv = document.createElement('div');
    appDiv.id = "myApp";
    appDiv.innerHTML = (isSecondTime ? ' 2nd bootstrap': ' 1st bootstrap') + template.innerHTML;
    document.body.appendChild(appDiv);
    angular.bootstrap(angular.element(appDiv), ['myApp']);

    return appDiv;
}

var createdAppDiv = bootstrapApp(null);

setTimeout(function() {
    console.log("try boostraping again");
    bootstrapApp(createdAppDiv);
}, 5000);
like image 155
Jack Avatar answered Apr 08 '23 22:04

Jack