When I dynamically add HTML using ng-bind-html how

2019-08-05 04:22发布

问题:

My index.html contains the following element

...
<div class="col-md-6" ng-bind-html='module.docs.html'></div>
...

The module.docs.html property returns HTML like

<div ng-controller="accordion-demo-ctrl">
  <label class="checkbox">
    <input type="checkbox" ng-model="oneAtATime">
    Open only one at a time
  </label>

  <accordion close-others="oneAtATime">
    <accordion-group heading="Static Header, initially expanded" is-open="true">
      This content is straight in the template.
    </accordion-group>
    <accordion-group heading="{{group.title}}" ng-repeat="group in groups">
      {{group.content}}
    </accordion-group>
    <accordion-group heading="Dynamic Body Content">
      <p>The body of the accordion group grows to fit the contents</p>
        <button class="btn btn-default btn-sm" ng-click="addItem()">Add Item</button>
        <div ng-repeat="item in items">{{item}}</div>
    </accordion-group>
    <accordion-group is-open="isopen">
        <accordion-heading>
            I can have markup, too! <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isopen, 'glyphicon-chevron-right': !isopen}"></i>
        </accordion-heading>
        This is just some content to illustrate fancy headings.
    </accordion-group>
  </accordion>
</div>

The HTML is inserted but Angular.dart seems not to do anything with it (accordion-demo-ctrl is not instantiated)

回答1:

Using the ng-include directive worked. I changed my code so that I provided the URI of the HTML fragment instead of the source itself (fetched from the server anyway).

see also my answer to How to add a component programatically in Angular.Dart? for an alternative solution