Angular Tabs Break使用ng-bind-html时

时间:2014-02-05 15:36:29

标签: javascript html angularjs twitter-bootstrap

我正在使用带有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>

1 个答案:

答案 0 :(得分:0)

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

将html内容解析为可信赖的html,以便安全..比如

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

ng-bind-html 中使用受信任的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>

试试这个......