【问题标题】:AngularJS - Styles not applying on appended htmlAngularJS - 样式不适用于附加的 html
【发布时间】:2014-11-19 05:32:50
【问题描述】:

我想注入一个 html,它是渲染 django 模板的结果。 Django 模板:

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

硬编码时生成的 html 会正确显示。 我想将收到的 html 代码注入到 div 中。问题是引导手风琴组件不显示。我只得到文本。

角度控制器中的代码:

$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代码:

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

myHTML 包含这样的内容:

<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>

上面的代码只是呈现为一个垂直列表菜单,但没有折叠。如果加载为静态 html 文件,相同的代码可以很好地呈现。 我正在使用 Angular 的 bootstrap

【问题讨论】:

    标签: jquery html css django angularjs


    【解决方案1】:

    这个问题有两种可能的答案:

    DEMO

    [1] 如果您的 dataService.getHtml() 实际上是从返回 html 的 url 从服务器请求数据,并且它没有在服务中操作 html 本身,那么您可以安全地使用ng-include 指令:

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

    如果是这种情况,那么你就不需要实际使用$compile服务并在html中手动添加。

    [2] 如果您的 dataService 不需要来自服务器的请求,并且正在服务本身内部构建 html 内容,那么您需要使用$compile 服务。为此,您需要创建一个编译 html 内容并手动添加 html 本身的指令。

    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);
            }
          });
        }
      });
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-13
        • 1970-01-01
        • 2021-07-30
        • 1970-01-01
        • 2017-06-17
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多