ngBind {{}} ngBindTemplate
1.首先我们最常使用的一个绑定表达式的指令是ngBind,比如在一个div标签中我们可以这样使用:
<div ng-bind="vm.info"></div>
这样就把控制器中的vm.info的值绑定到这个div标签里面了,使用这个指令还有一个好处就是在页面还没有完全渲染好的情况下,是不会出现Angular的{{}}解析符号的,
它隐藏了Angular解析表达式的过程。如果你使用了下面的方法,
<div>{{vm.info}}</div>
在网速不是很好的情况下,就会出现{{}}解析符号,给用户带来不好的体验。
2.下一个绑定表达式的指令就是ngBindTemplate了,这个指令与上一个指令的最大不同之处是:ngBindTemplate可以绑定多个表达式,这样就有一个好处,在一些元素中,
比如title和option是不可以包含sapn元素的,这时候如果需要多个变量的话,ngBindTemplate就是很必须的。
<div ng-bind-template="{{vm.info}} {{vm.msg}}"></div>
还要注意一点就是,这个指令的用法,多个模型变量是用{{}}括起来的。
3.接下来的一些是关于模板的绑定,第一个比较常用的是ngBindHtml,从指令的名字就可以看出来,这个指令是绑定一段html的,那么这个指令该如何使用呢?我们来研究一下,首先,
我们需要在控制器定义一段html代码,如下所示:
vm.html = '<div class="container">\
<div class="title">{{vm.info}}</div>\
<div class="content">content</div>\
</div>';
然后我们就会很自然地想到按照下面的方法去使用这个指令:
<div ng-bind-html="vm.html"></div>
但是当你在浏览器上运行的时候,却发现浏览器给你报了一个错误,如下所示:
说你在一个安全的上下文中使用了不安全的值,怎么解决这个问题呢?我们可以手动的将我们定义的那段html代码,变成Angular信任的值,具体的方法是在控制器定义一个方法如下所示:
function trust_my_html(str){
return $sce.trustAsHtml(str);
}
然后在html中可以这样使用:
<div ng-bind-html="vm.trust_my_html(vm.html)"></div>
4.上面的方法在一定程度上解决了模板的绑定问题,但是,还有一个小问题,就是模板中的表达式并没有被解析,你可以看看我写的那个demo,所以要想解决这个问题,就是有两个办法,
首先就是你自己写一个指令,当绑定模板的时候,将模板中的表达式也给解析了,第二个办法就是使用别人的插件,我看到一个比较好的插件,名字叫做angular-bind-html-compile,
我们在我们的主模块中注入这个依赖就可以使用这个指令了,使用方法如下:
<div bind-html-compile="vm.html"></div>
这样一来,我们模块中的表达式也可以被解析了。还有需要注意,使用这个指令就不需要我们手动的将那段html片段变成Angular信任的值了。
5.当我们仔细看了看上面所说的那个指令,发现也不是那么的难,还不如我们自己写一个呢,不然还要引入他的文件,太费事了,具体的代码如下:
function compileBindHtml($compile){
var directive = {
restrict: 'AE',
link:linkFunc
};
return directive;
function linkFunc(scope, elements, attrs){
var func = function(){
return scope.$eval(attrs.compileBindHtml);
};
scope.$watch(func, function(newValue){
elements.html(newValue);
$compile(elements.contents())(scope);
})
}
}
我们来看看这个指令,在链接函数中,我们使用$watch监测func函数的返回值,func函数的返回值是一个被$eval的属性值,也就是我们的模板值,然后当检测到有变化的时候,
就将我们的模板值放置到含有这个指令的html标签中,然后在使用$compile服务将我们的模板给编译了。看看其实也不是那么难的。
6.最后一个可以用来绑定模板的指令是ngInclude,这个指令使用的频率相对来说比较高一点,那么这个指令怎么使用呢?我们一起来研究一下。
方法一,将模板写在html文件中,这个过程要通过使用script指令来实现,如下所示的一个例子:
<script type="text/ng-template" id="template-1">
<div class="container">
<div class="title">{{vm.info}}</div>
<div class="content">content</div>
</div>
</script>
这里来讲解一下怎么使用这个script指令,首先它的type属性值必须是ng-template,然后id值是它的一个标记或者索引,当你需要在html中使用它的时候就要使用这个id的值来引用这段模板。
方法二,将模板写在js文件中,通过使用$templateCache服务,来写我们的模板,具体的用法如下:
function configFuc($templateCache){
var template = '<div class="container">\
<div class="title">{{vm.info}}</div>\
<div class="content">content</div>\
</div>';
$templateCache.put('template-2', template);
}
我们的模板的索引是template-2,具体的内容就是template变量里面的内容。
在html里面使用的方法如下所示:
<div ng-include="'template-1'"></div>
<div ng-include="'template-2'"></div>
使用ngInclude的另一个好处就是模板里面的表达式是会被解析的。
完整的代码:
index.html
<body ng-controller="MyController as vm">
<script type="text/ng-template" id="template-1">
<div class="container">
<div class="title">{{vm.info}}</div>
<div class="content">content</div>
</div>
</script>
<h1 class="title">ngInclude <span>VS</span> ngBindHtml <span>VS</span> ngBind <span>VS</span> ngBindTemplate</h1>
<hr/>
<h3>ngBind</h3>
<div ng-bind="vm.info"></div>
<hr/>
<h3>ngBindTemplate</h3>
<div ng-bind-template="{{vm.info}} {{vm.msg}}"></div>
<hr/>
<h3>ngBindHtml</h3>
<!-- 这里需要加上这个trust函数 不然会被当做不安全的html 然后会报错-->
<div ng-bind-html="vm.trust_my_html(vm.html)"></div>
<!-- 下面的不需要在外层加上那个trust函数 ,不然会报错-->
<div compile-bind-html="vm.html"></div>
<!--可以使用别人写好的一个插件-->
<!--https://github.com/incuna/angular-bind-html-compile-->
<div bind-html-compile="vm.html"></div>
<hr/>
<h3>ngInclude</h3>
<!-- 加载的模板第一种方法是通过`$templateCache`服务 -->
<div ng-include="'template-1'"></div>
<!-- 加载模板的第二种方法是通过`script`指令 -->
<div ng-include="'template-2'"></div>
</body>
index.js
(function(){
angular.module('MyApp', ['angular-bind-html-compile'])
.run(configFuc)
.controller('MyController', MyController)
.directive('compileBindHtml', compileBindHtml);
configFuc.$inject = ['$templateCache'];
MyController.$inject = ['$sce'];
compileBindHtml.$inject = ['$compile'];
function configFuc($templateCache){
var template = '<div class="container">\
<div class="title">{{vm.info}}</div>\
<div class="content">content</div>\
</div>';
$templateCache.put('template-2', template);
}
function MyController($sce){
var vm = this;
vm.info = 'Hello,World';
vm.msg = 'Thank You!';
vm.html = '<div class="container">\
<div class="title">{{vm.info}}</div>\
<div class="content">content</div>\
</div>';
vm.trust_my_html = trust_my_html;
vm.get_trust_html = get_trust_html;
function trust_my_html(str){
return $sce.trustAsHtml(str);
}
function get_trust_html(str){
return $sce.getTrustedHtml(str);
}
}
function compileBindHtml($compile){
var directive = {
restrict: 'AE',
link:linkFunc
};
return directive;
function linkFunc(scope, elements, attrs){
var func = function(){
return scope.$eval(attrs.compileBindHtml);
};
scope.$watch(func, function(newValue){
elements.html(newValue);
$compile(elements.contents())(scope);
})
}
}
})();
style.css
h1.title{
text-align: center;
}
h1.title span{
color: #CCC;
}
.container{
width: 100%;
height: 60px;
}
.container .title{
height: 20px;
background-color: #b3d4fc;
font-size: 20px;
line-height: 20px;
text-align: center;
}
.container .content{
height: 40px;
background-color: #0000FF;
font-size: 15px;
line-height: 40px;
text-align: center;
}
h3{
text-align: center;
color: #FF0000;
}
div{
text-align: center;
}
参考的资料:How to make ng-bind-html compile angularjs code
ngBind {{}} ngBindTemplate的更多相关文章
- ngBind ngBindTemplate ngBindHtml
ng-bind: 只能绑定一个变量 在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: <p>{{titleStr}}</p> 另一种是使用基于属 ...
- angularJS内置指令一览
基础ng指令 ng-href ng-src ng-disabled ng-readonly ng-checked ng-selected ng-class ng-style ng-show ng-hi ...
- 流行框架angular
---恢复内容开始--- 一.angular是什么 一款非常优秀的前端高级js框架,由谷歌团队负责开发 angular是通过新的属性和表达扩展了html angular可以构建一个单一页面应用程序(s ...
- angularJS之ng-bind与ng-bind-template的区别
ng-bind-template 指令用于告诉 AngularJS 将给定表达式的值替换 HTML 元素的内容. 当你想在 HTML 元素上绑定多个表达式时可以使用 ng-bind-template ...
- angular.js ngbind nghtml ngTemplate
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- ng-bind,ng-cloak优化数据显示
<div>{{text}}</div> 当我们使用angular在页面中有取值的时候,如果出现网络加载慢的问题,可能会在页面上出现{{text}}这种不好的体验,那么angul ...
- angularjs之ng-bind和ng-model
1.为什么其他标签可以用ng-bind ,而input标签要用ng-model 这就是所谓的数据双向绑定,input是用于用户输入的,数据要从View传输到Controller中,而{{}}和ng-b ...
- ng-bind 与ng-model区别
双向绑定,一般来说是这样 <input ng-model="object.xxx"> <span ng-bind="object.xxx"&g ...
- angularJS——ng-bind指令与插值的区别
在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: <p>{{text}}</p> 另一种是使用基于属性的指令,叫做ng-bind: <p ...
随机推荐
- #include <NOIP2009 Junior> 细胞分裂 ——using namespace wxl;
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...
- 更改DNS轻松访问google.com,FaceBook,Youtube等
将默认的Dns更改为42.120.21.30即可打开 https://www.google.com/ https://www.facebook.com/ https://www.youtube.com ...
- CSS兼容性解决方法!important的IE7,Firefox问题
转自:http://www.codesky.net/article/201008/139903.html 1. 首先谈谈!important问题的引起(盒模型问题): 在CSS标准中,一个盒模型包括4 ...
- 【Android实战】----基于Retrofit实现多图片/文件、图文上传
本文代码详见:https://github.com/honghailiang/RetrofitUpLoadImage 一.再次膜拜下Retrofit Retrofit不管从性能还是使用方便性上都非常屌 ...
- Unity 事件2
UIMouseEvent.cs: using UnityEngine; using System; public abstract class UIMouseEvent : MonoBehaviour ...
- octopress github 换电脑 使用
octopress github 换电脑 使用
- ReSharper的功能真的很强大主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚
二.ReSHarper 代码规范.单元测试.... ReSharper的功能真的很强大,主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚! 1.像命名不规范,f ...
- LeetCode_Path Sum
一.题目 Path Sum My Submissions Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- 清除tomcat的缓存
删除tomcat目录下的work目录中的Catalina目录就好了!
- ssh登陆不上
用ssh key登陆不上某台机A的某个账号xy1,查看A的/var/log/messages,看到有这么句: User xy1 not allowed because account is locke ...