简体   繁体   中英

How to add a new module dynamically?

Image that I have the following .js code:

 $.ajax({
     url: 'some/url',
     success: function(result) {
        $activeTabContainer.html(result);
    }
 });

The $activeTabContainer is jQuery selected div.

The result object contains html (with script tag), maybe something like:

<div ng-app="mySpa">
    <div ng-controller="myController"></div>
</div>

And later in the same .html file a script tag, which contains:

(function () {
    "use strict";
    var app = angular.module("mySpa", []);
    app.controller("myController", [function () {
        debugger;
    }]);
})();

It perfectly executes the .js part in the script tag, but I guess that angular must be somehow notified about the dynamically added module and its controllers, something like dynamically bootstrapping.

Do anyone have an idea?

I found the solution, it happens because angular does not bootstrap automatically, when a new module is added dynamically. You need to bootstrap it manually:

angular.bootstrap(document, ["mySpa"]);

And bingo it works :-)

Link to the documentation: https://docs.angularjs.org/api/ng/function/angular.bootstrap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM