angularjs中使用ng-bind-html和ng-include的实例
下面这个例子,往div标签内添加html内容:
<!doctype html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>demo</title> <script type="text/javascript" src="lib/angularjs/angular.min.js" charset="utf-8"></script> </head> <body ng-controller="ctrl"> <div ng-bind-html="myText"></div> <script> var app = angular.module('myApp',[]); app.controller("ctrl",function($scope,$sce){ $scope.myText = $sce.trustAsHtml("<b>粗体内容</b><br/><i>斜体内容</i>"); }); </script> </body> </html>
注意在赋值时需要将html用$sce.trustAsHtml作一个处理,不然页面会报错,无法输出内容。
下面是ng-include的使用示例,跟上面的例子差不多,只是把要添加的html内容放到一个文件中:
<!doctype html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>demo</title> <script type="text/javascript" src="lib/angularjs/angular.min.js" charset="utf-8"></script> </head> <body ng-controller="ctrl"> <div ng-include="'demo1_1.html'"></div> <script> var app = angular.module('myApp',[]); app.controller("ctrl",function($scope){}); </script> </body> </html>
demo1_1.html
<b>粗体内容222</b><br/><i>斜体内容222</i>
相关推荐
xiaoge00 2020-05-17
wenjieyatou 2020-05-04
spinachcqb 2020-03-06
muzirigel 2020-02-02
tanyhuan 2020-02-01
wwwxuewen 2020-01-19
jocleyn 2020-01-13
aolishuai 2020-01-07
魏莉的微 2020-01-07
byourb 2019-12-23
First00 2019-12-23
行吟阁 2019-12-15
iviewer是一个具有缩放和图像旋转功能的图像查看小部件。在jQuery官网下载后,有很多文件。直接把文件夹解压拖到项目里。然后再html中引入主要的文件。-- iviewer-鼠标滚轮js -->
xiaoge00 2019-12-07
行吟阁 2019-12-07
WebVincent 2019-11-19