Dynamically creating an angular view

前端 未结 2 1912
慢半拍i
慢半拍i 2020-12-13 00:56

I\'m making an in game UI using awesomium, at some points the game loads up and executes a chunk of javascript which is meant to create arbitrary new UI elements. e.g.

相关标签:
2条回答
  • 2020-12-13 01:27

    You might want to use ng-include combined with ng-repeat. Here is an simple example: http://plunker.no.de/edit/IxB3wO?live=preview

      <div ng-repeat="dom in domList" ng-include="dom"></div>
    

    Parent $scope will keep the list of partials loaded into the view. And ng-repeat + ng-include will iterate over and display partials according to the list.

    When it is the right timing, you can append the partial into the dom list. e.g.

    $scope.domList.push("chatbox.html");
    

    (BTW, putting DOM manipulation into controller is not the angular way.)

    0 讨论(0)
  • 2020-12-13 01:33

    You should $compile dynamically added angular content. Something like:

    jQuery(document.body).append(
        $compile(  
            '<div ng-controller="ChatBoxControl"><div ng-repeat="line in chat"><span>{{line}}</span></div></div>'
        )(scope)
    );
    

    scope for any element you can get using something like:

    var scope = angular.element('#dynamicContent').scope();
    

    Also you should get $compile that can be injected in other controller.

    See also: AngularJS + JQuery : How to get dynamic content working in angularjs

    0 讨论(0)
提交回复
热议问题