繁体   English   中英

AngularJS ng-bind-html 不呈现列表标签

[英]AngularJS ng-bind-html not rendering list tags

我有一个简单的 HTML 文档,例如...

<ol>
    <li><strong>Test 1</strong></li>
    <li><strong>Test 2</strong></li>
</ol>

...我想用ng-bind-html将它绑定到一个div ,但是 HTML 标签<ol> (或<li> )没有呈现。

我的结果是:

<strong>Test 1</strong>
<strong>Test 2</strong>

有任何想法吗?

为了使用 ng-bind-html,你需要在你的应用声明中包含 ngSanitize

 * { padding: 0; margin: 0; } ol li { list-style: none; }
 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-sanitize.js"></script> <body> <div ng-app="myApp" ng-controller="myCtrl"> <div ng-bind-html="myText"></div> </div> <script> var app = angular.module("myApp", ['ngSanitize']); app.controller("myCtrl", function($scope) { $scope.myText = "<ol><li><strong>Test 1</strong></li><li><strong>Test 2</strong></li></ol>"; }); </script> </body> </html>

ng-bind-html指令完成了这一切。 更多信息,请访问https://docs.angularjs.org/api/ng/directive/ngBindHtml

 <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="myCtrl"> <span ng-bind-html="trustedHtml"></span> </div> <script> var app = angular.module("myApp",[]); app.controller("myCtrl",["$scope","$sce", function($scope,$sce) { $scope.html = '<ol><li><strong>Test 1</strong></li><li><strong>Test 2</strong></li></ol>'; $scope.trustedHtml = $sce.trustAsHtml($scope.html); }]); </script> </body> </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM