AngularJS 不得不了解的服务 $compile 用于动态显示html内容
项目中一度纠结与AngularJS如何动态显示不同的html内容。
本来是希望直接使用下面的语句来实现:
<div> </div>
但是很尴尬的是,这样不能识别出html标签,而是直接将html里的页面标签全都显示出来了。这当然不是我想要的效果。
谷哥了一番,没想到在官网上就找到了我想要实现的效果,而实现的主角就是今天的 $compile 服务。
https://docs.angularjs.org/api/ng/service/$compile
节选一下关键部分内容,Javascript:
<script>
angular.module('compileExample', [], function($compileProvider) {
// configure new 'compile' directive by passing a directive
// factory function. The factory function injects the '$compile'
$compileProvider.directive('compile', function($compile) {
// directive factory creates a link function
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
// watch the 'compile' expression for changes
return scope.$eval(attrs.compile);
},
function(value) {
// when the 'compile' expression changes
// assign it into the current DOM
element.html(value);
// compile the new DOM and link it to the current
// scope.
// NOTE: we only compile .childNodes so that
// we don't get into infinite loop compiling ourselves
$compile(element.contents())(scope);
}
);
};
});
})
.controller('GreeterController', ['$scope', function($scope) {
$scope.name = 'Angular';
$scope.html = 'Hello ';
}]);
</script>
Html:
<div ng-controller="GreeterController">
<input ng-model="name"> <br>
<textarea ng-model="html"></textarea> <br>
<div compile="html"></div>
</div>
总之就是用$compile服务创建一个directive ‘compile’,这个complie会将传入的html字符串或者DOM转换为一个template,然后直接在html里调用compile即可。
原文出处:http://gsgundam.com/2014-12-13-angularjs-compile-to-show-dymanic-html-content/
AngularJS 不得不了解的服务 $compile 用于动态显示html内容的更多相关文章
- inux中tail命令---用于查看文件内容
linux中tail命令---用于查看文件内容 最基本的是cat.more和less.1. 如果你只想看文件的前5行,可以使用head命令,如:head -5 /etc/passwd2. 如果你想查看 ...
- linux命令之----sort命令用于将文本文件内容加以排序
1.sort命令作用 sort命令用于将文本文件内容加以排序,将输入行按照键值字段与数据类型选项以及locale排序. 一个可预期的记录次序,会让用户的查看使用更方便:书的索引.字典.目录以及电话簿等 ...
- Visual Studio Code 帮助查看器,指定的用于安装帮助内容的位置无效,或者您无权访问该位置
今天有个C# 类库文件里面的属性想要了解下,想到了Vs的帮助文档,其实也就是微软的MSDN:提示帮助查看器,指定的用于安装帮助内容的位置无效,或者您无权访问该位置: 最近两天vs也没有更新,并且也没有 ...
- AngularJS——第8章 服务
第8章 服务 服务是一个对象或函数,对外提供特定的功能. 8.1 内建服务 1. $location是对原生Javascript中location对象属性和方法的封装. // $location内置服 ...
- AngularJS中实现日志服务
本篇体验使用AngularJS自定义一个记录日志的服务. 在AngularJS中,服务的一些写法是这样的: var app = angular.module('app',[]); app.provid ...
- angularJS中如何写服务
服务的用途 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性 服务提供了把特定功能相关联的方法集中在一起的接口 如何创建服务 angularJS ...
- angularJS module里的'服务'
首先,为了举栗子,先写好如下的模型,控制器,html: html: <!DOCTYPE html> <html ng-app="serviceApp"> & ...
- 自定义AngularJS中的services服务
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- AngularJs(Part 3)--注册服务
有以下5中方法注册一个AngularJS可以识别的Service value和constant是两个极其简单的方法,只有少数情况下会使用. service已经开始复杂了起来.而factory是我认为既 ...
随机推荐
- Java程序的成长之路
转载链接:http://www.admin10000.com/document/2901.html 互联网发展日新月异,社会科技每天都在发生着翻天覆地的变化,而程序员已经成了这个时代的庞大群体,各种各 ...
- debian修改系统语言为英文
原文地址:http://www.chenyudong.com/archives/debian-change-locale-language.html 修改/etc/default/locale 文件里 ...
- vim 常用快捷键 二[转]
键盘移动 (Move) 一切都从键盘的移动k -> 上 upj -> 下 downh -> 左 leftl -> 右 rightz -> 重画屏幕,当前光标变成屏幕的第一 ...
- Python学习(七) 流程控制if语句
在Python中流程控制if语句采用如下格式: if expression : statement elif expression : statement elif expression : stat ...
- Html 编码 queryUrl = encodeURI(queryUrl);
Html 编码 queryUrl = encodeURI(queryUrl);
- ES6新特性-------解构、参数、模块和记号(续)
六.解构 解构提供了一个方便的地从对象或数组中提取数据的方法,请看下面的例子: //ES6 let [x,y]=[1,2];//x=1,y=2 //ES5 var arr=[1,2]; var x=a ...
- Document原来可以这样来获取DOM
images集合(页面中的图象) a)通过集合引用 代码 document.images //对应页面上的<img>标签 document.images.leng ...
- scheme代码高亮
(defun iedit-symbol-in-defun () "Enter `iedit-mode' to rename the symbol in current function, o ...
- QAction系列详解
QAction系列详解 一.QAction类详解 [详细描述] QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中. 应用程序可以通过菜单,工具栏按 ...
- 2015第10周三jquery ui position
jQuery UI API - .position() 所属类别 方法重载(Method Overrides) | 方法(Methods) | 实用工具(Utilities) 用法 描述:相对另一个元 ...