简体   繁体   中英

AngularJS - Styles not applying on appended html

I want to inject a html that is a result of rendering a django template. Django template:

class CView(View):
    def get(self, request):
        return render_to_response('my_template.html', {},content_type="application/json")

The resulted html displays correctly when hardcoded. I want to inject the received html code into the a div. The problem is that the bootstrap accordion component doesn't display. I only get the text.

code in the angular controller:

$scope.myHTML = '<div>Loading..<div>';
$scope.to_trusted = function (html_code) {
    return $sce.trustAsHtml(html_code);
}
dataService.getMyHtml().success(function (data) {
    $timeout(function () {
        $scope.$apply(function () {
            $scope.myHTML = data;
        })
    }, 0);
}).error();

html code:

 <div ng-bind-html="to_trusted(myHTML)"></div>

myHTML holds something like this:

<div class="col-sm-3 col-md-2 sidebar">
    <accordion close-others="true">
        <accordion-group>
            <accordion-heading>
                Items One <i class="pull-right glyphicon"
                           ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
            </accordion-heading>
            <ul class="nav nav-sidebar">
                <li><a href="#/one">One 1</a></li>
                <li><a href="#/two">One 2</a></li>
            </ul>
        </accordion-group>

        <accordion-group>
            <accordion-heading>
                Items Two <i class="pull-right glyphicon"
                           ng-class="{'glyphicon-chevron-down': status.open, 'glyphicon-chevron-right': !status.open}"></i>
            </accordion-heading>
            <ul class="nav nav-sidebar">
                <li><a href="#/one">Two 1</a></li>
                <li><a href="#/two">Two 2</a></li>
            </ul>
        </accordion-group>     
    </accordion>
</div>

The above code just renders as a vertical list menu, but no accordion. The same code renders fine if loaded as a static html file. I am using angular's bootstrap .

There are two possible answers to this problem:

DEMO

[ 1 ] If your dataService.getHtml() is actually requesting data from the server from a url which returns an html, and it is not manipulating the html itself in the service, then you can safely use the ng-include directive:

<div ng-include="'http://somewhere/mytemplate.html'"></div>

If this is the case, then you don't need to actually use the $compile service and manually add it in the html.

[ 2 ] If your dataService does not require a request from the server and is building an html content inside the service itself, then you need to use the $compile service. To do this, you need to create a directive that compiles the html content and manually add the html itself.

HTML

<div compile-html="html"></div>

JAVASCRIPT

  .service('dataService', function($http, $q) {
    this.getHtml = function() {
      var deferred = $q.defer();
      $http.get('accordion.tpls')
        .success(function(template) {
          deferred.resolve(template + '<h1>Added another html in here</h1>');
        }, deferred.reject);
      return deferred.promise;
    };
  })

  .controller('Ctrl', function($scope, dataService) {

    dataService.getHtml().then(function(template) {
      $scope.html = template;
    });

  })


  .directive('compileHtml', function($compile) {
    return function(scope, elem, attr) {
      scope.$watch(attr.compileHtml, function(value) {
        if(value) {
          elem.html(value);
          $compile(elem.contents())(scope);
        }
      });
    }
  });

你必须在angular中使用$ compile函数才能识别注入的html代码中的指令。

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