【问题标题】:Display entire html file using angularjs?使用angularjs显示整个html文件?
【发布时间】:2017-07-14 22:44:04
【问题描述】:

我正在尝试使用 Spring Boot 和 AngularJS 开发应用程序。这里的问题是 HTML (Twitter Bootstrap 3) 文件存储在 DB 中。而且我不知道如何使用 AngularJS 加载/显示这个 HTML 文件。

到目前为止,我的 AngularJS 控制器如下所示:

myApp.controller('initCtrl', function($scope,$http) {
    console.log('init ctrl');
    $scope.info= {};
    $scope.init= function(){
        console.log($scope.info);
        $http.post('load',$scope.info).then(function(response){
            console.log(response.data);//Getting Entire HTML file using Spring Controller from db
            //enter code here
        });
    };
});

我不能使用<div ng-include="{{template}}"></div>,因为我不能在<div> 元素中包含整个HTML。并且 HTML 文件将使用headersfooters 非常频繁地更新。所以除了渲染它并在浏览器中显示之外,我对 HTML 无能为力。我怎样才能做到这一点?

【问题讨论】:

  • 你不是已经在 HTML 范围内了吗!?
  • 是的,但范围不同。
  • 我不明白如何将 放在 中,这样它就可以工作了...您显示将 的内部注入当前正文中的某处.. .
  • 我不想将 HTML 保存在 HTML 中。我想要一个单独的 HTML 来显示。
  • $(html).find('body').html() 像这样??

标签: angularjs


【解决方案1】:

这很简单...

这是您的更新代码。

Contoroller

myApp.controller('initCtrl', function($scope,$http) {
    console.log('init ctrl');
    $scope.info= {};
    $scope.init= function(){
        console.log($scope.info);
        $http.post('load',$scope.info).then(function(response){
            console.log(response.data);//Getting Entire HTML file using Spring Controller from db
            $scope.renderHtml = response.data;
        });
    };
});

HTML

<div ng-bind-html="renderHtml"></div>

【讨论】:

  • 您应该只将 部分存储在数据库中。然后在
    中渲染它
  • 这不取决于我。更新数据库的应用程序不同。
【解决方案2】:

您可以使用 ng-bind-html 将 HTML 绑定到 div

<div ng-show="htmlFlag" ng-bind-html="htmlData"></div>

在您的控制器中:

myApp.controller('initCtrl', function($scope,$http) {
    console.log('init ctrl');
    $scope.htmlFlag = false;
    $scope.info= {};
    $scope.init= function(){
        console.log($scope.info);
        $http.post('load',$scope.info).then(function(response){
            console.log(response.data);
            $scope.htmlFlag = false; 
            $scope.htmlData = response.data;
            $scope.htmlFlag = true;
        });
    };
});

【讨论】:

  • 我不能说你不想这样做,或者你的代码不能这样工作?
  • 这行得通。每当您的 HTML 在数据库中发生变化时。您可以通过重新加载直接在页面上看到效果。
  • 我并不是说不可能使用这种方法,因为它仅适用于内部 Html。但就我而言,我必须加载整个 HTML 页面,而不仅仅是几行或 div。
猜你喜欢
  • 2014-06-07
  • 1970-01-01
  • 2019-01-11
  • 2012-04-19
  • 1970-01-01
  • 2023-03-07
  • 2014-08-02
  • 1970-01-01
  • 2020-09-10
相关资源
最近更新 更多