【问题标题】:Angular Tabs Break When using ng-bind-html使用 ng-bind-html 时 Angular Tabs Break
【发布时间】:2014-12-27 16:21:52
【问题描述】:

我正在使用带有 bootstrap 和 angular-text 模块的 angular ui 来构建选项卡生成器
我正在使用 ng-bind-html 来允许来自 angular-text 模块的选项卡内容中的 html
问题是这样做后我的布局被破坏了
我该如何解决这个问题?
这是代码

        <!DOCTYPE HTML>
    <html lang="en-US" ng-app="myModule">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet prefetch" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
        <link rel='stylesheet prefetch' href='http://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.min.css'>
        <link rel="stylesheet" href="css/styles.css">
        <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js'></script>
        <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular-sanitize.min.js'></script>
        <script src="js/ui-bootstrap-tpls-0.10.0.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/textAngular/1.1.2/textAngular.min.js"></script>
 <script>

 angular.module('myModule', ['ui.bootstrap','textAngular', 'ngSanitize']);

var TabsCtrl = function ($scope) {
  $scope.tabs = [];

  $scope.addTab = function() {
     $scope.tabs.push({
      title: $scope.tab_heading ,
      content:$scope.tab_content 
     });
    };
   };
   </script>
        <title>Angular UI</title>
    </head>
    <body>

    <div class="tbh" ng-controller="TabsCtrl">
      <tabset>

        <tab  ng-repeat="tab in tabs" heading="{{tab.title}}" ng-bind-html="tab.content" active="tab.active" disabled="tab.disabled">

        </tab>
      </tabset>
      <hr/>
       <h3>Add Tab</h3>
       <label>Tab Title</label><input type="text" ng-model="tab_heading" class="form-control"/><br/><br/>
       <label>Tab Content</label>
          <div text-angular="text-angular"  ng-model="tab_content"></div>
       <br/><br/>

       <button class="btn-info btn" ng-click="addTab(); tab_content ='';tab_heading='';">Add Tab</button>

    </div>
    </body>
    </html>

【问题讨论】:

    标签: javascript html angularjs twitter-bootstrap


    【解决方案1】:

    ng-bind-html 你必须使用 $sanitize 服务来解析标签内容,例如 ..

    将 html 内容解析为受信任的 html 以确保安全......就像

    $scope.trustedHtml = function (html_code) {
          return $sce.trustAsHtml(html_code);
      } 
    

    ng-bind-html..like..

    中使用受信任的html
    ng-bind-html="trustedHtml(tab.content)"
    

    工作.....

             <!DOCTYPE HTML>
        <html lang="en-US" ng-app="myModule">
        <head>
            <meta charset="UTF-8">
            <link rel="stylesheet prefetch" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
            <link rel='stylesheet prefetch' href='http://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.min.css'>
            <link rel="stylesheet" href="css/styles.css">
            <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js'></script>
            <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular-sanitize.min.js'></script>
            <script src="ui-bootstrap-tpls-0.12.0.min.js"></script>
            <script src="http://cdnjs.cloudflare.com/ajax/libs/textAngular/1.1.2/textAngular.min.js"></script>
     <script>
    
     angular.module('myModule', ['ui.bootstrap','textAngular', 'ngSanitize']);
    
     var TabsCtrl = function ($scope, $sce) {
         $scope.tabs = [];
    
      $scope.trustedHtml = function (html_code) {
          return $sce.trustAsHtml(html_code);
      }
      $scope.addTab = function() {
         $scope.tabs.push({
          title: $scope.tab_heading ,
          content:$scope.tab_content 
         });
        };
       };
       </script>
            <title>Angular UI</title>
        </head>
        <body>
    
        <div class="tbh" ng-controller="TabsCtrl">
          <tabset>
    
              <tab ng-repeat="tab in tabs" heading="{{tab.title}}" ng-bind-html="trustedHtml(tab.content)" active="tab.active" disabled="tab.disabled">
    
              </tab>
          </tabset>
          <hr/>
           <h3>Add Tab</h3>
           <label>Tab Title</label><input type="text" ng-model="tab_heading" class="form-control"/><br/><br/>
           <label>Tab Content</label>
              <div text-angular="text-angular"  ng-model="tab_content"></div>
           <br/><br/>
    
           <button class="btn-info btn" ng-click="addTab(); tab_content ='';tab_heading='';">Add Tab</button>
    
        </div>
        </body>
        </html>
    

    试试这个...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-22
      • 2015-02-06
      • 2013-01-21
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 2015-08-31
      相关资源
      最近更新 更多