How to bind an AngularJS controller to dynamically added HTML?

后端 未结 6 625
清歌不尽
清歌不尽 2020-12-03 02:13

For this scenario, I have a page of HTML with some AngularJS directives, controllers, etc.

Something like this:



  
6条回答
  •  情书的邮戳
    2020-12-03 02:29

    I followed this process and it worked for me:

    // Some variables
    var $controllerElement = angular.element('css-selector-to-the-controller-element');
    var $appElement = angular.element('css-selector-to-ng-app-element');
    
    // compiling and applying / digesting the scope.
    $appElement.injector().invoke(function($compile) {
        var scope = $controllerElement.scope();
        $compile($controllerElement)(scope);
        scope.$apply();
    });
    

    Reference: Angular.injector

提交回复
热议问题