Initialize AngularJS controller after append some html into DOM

后端 未结 2 795
盖世英雄少女心
盖世英雄少女心 2020-12-17 02:12

Is there a way how to use $compile from pure javascript? I\'m combinating basic javascript and places where I\'m using Angular and can\'t find a way how to do something like

相关标签:
2条回答
  • 2020-12-17 02:38
    angular.bootstrap($("#targetElmForApp")[0], ["myApp"]);
    

    Explanation:

    angular.bootstrap(rootElementForApp, arrayOfModulesForTheApp)
    

    The first parameter sets the root element for the app, which is what you would do by using ng-app directive otherwise.

    The second parameters is a list of modules that defines the application. This is typically a single module in which the app is defined.

    So what we did is the same as:

    <div id="targetElmForApp" ng-app="myApp"></div>
    

    see:
    http://docs.angularjs.org/api/angular.bootstrap
    http://docs.angularjs.org/guide/bootstrap

    0 讨论(0)
  • 2020-12-17 02:53

    Alternative way from this post: AngularJS + JQuery : How to get dynamic content working in angularjs

            angular.element(document).injector().invoke(function ($compile) {
                    var container = $('#some-dom-element');
                    var scope = angular.element(container).scope();
                    $compile(container)(scope);
            });
    
    0 讨论(0)
提交回复
热议问题