Angular JS中自定义标签 属性绑定的解释
看到自定义标签的文档时,文档作者解释的能力实在太弱,也可能是本人太笨,一下绕不过来。 看了一个stackoverflow答案,才算明白,在此贴出翻译,以供大家参考。
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
1: app.directive('mytag',function() {
2: return {
3: restrict: 'E',
4: template: '<div>' +
5: '<input ng-model="controltype"/>' +
6: '<button ng-click="controlfunc()">Parent Func</button>' +
7: '<p>{{controlval}}</p>' +
8: '</div>',
9: scope: {
10: /* make typeattribute="whatever" bind two-ways (=)
11: $scope.whatever from the parent to $scope.controltype
12: on this directive's scope */
13: controltype: '=typeattribute',
14: /* reference a function from the parent through
15: funcattribute="somefunc()" and stick it our
16: directive's scope in $scope.controlfunc */
17: controlfunc: '&funcattribute',
18: /* pass a string value into the directive */
19: controlval: '@valattribute'
20: },
21: controller: function($scope) {
22: }
23: };
24: });
25:
26: <div ng-controller="ParentCtrl">
27: <!-- your directive -->
28: <mytag typeattribute="parenttype" funcattribute="parentFn()" valattribute="Wee, I'm a value"></mytag>
29: <!-- write out your scope value -->
30: {{parenttype}}
31: </div>
32:
33:
34: app.controller('ParentCtrl', function($scope){
35: $scope.parenttype = 'FOO';
36: $scope.parentFn = function() {
37: $scope.parenttype += '!!!!';
38: }
39: });
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
The magic is mostly in the scope:
declaration in your directive definition. having any scope: {}
in there will "isolate" the scope from the parent, meaning it gets it's own scope... without that, it would use the parent's scope. The rest of the magic is in the scope's properties: scope: { 'internalScopeProperty' : '=externalAttributeName' }
... where the =
represents a two way binding scenario. If you change that =
to a @
you'll see it just allows you to pass a string as an attribute to the directive. The &
is for executing functions from the parent scope's context.
I hope that helps.
窍门在scope: 在你自己的标签定义中,声明的任何 scope : {} 都会从父scope中‘隔离’出只属于自己的scope, 如果没有 scope: {}定义,就会直接使用父scope,其余的特点就是scope属性定义, scope{ internalScopeProperty’ : ‘=externalAttributeName’ } 这里externalAttributeName是属性名, =代表双向绑定的场景, 如果把 = 改成 @, 你就只允许向标签传递一个字符串(这个后面在详细讲), 而 & 在这里就是执行一个父scope上下文的function。
I struggled a bit with this documentation too when first getting into angular, but I will make an attempt try to clarify things for you. First, when using this scope
property, it creates an "isolated scope." All this means is that it won't inherit any properties from parent scopes, and so you don't have to worry about any collisions within the scope.
在入门angular开始读到本文档时,我也遇到一些坑,不过我将试图在下面解释清楚。 首先当使用scope时, 它创建的是一个‘隔离的scope’, 这就意味着它不从父scope继承任何属性, 这就是说你不需从父scope哪里继承任何属性, 你不需要担心在独立隔离的scope里发生任何冲突
Now, the '@' notation means that the evaluated value in the attribute will automatically get bound into your scope for the directive. So, <my-directive foo="bar" />
would end up with the scope having a property called foo
that holds the string "bar". You could also do something like <my-directive foo="{{bar}}"
And then the evaluated value of {{bar}}
will be bound to the scope. Since attributes are always strings, you will always end up with a string for this property in the scope when using this notation.
现在‘@’符号表示标签里经过求值的属性会自动绑定到你的scope里面。 因此, <my-directive foo=”bar” />将会在scope里有个foo的属性装着字符串“bar”, 你可能也想这样写<my-directive foo=”{{bar}}”/>, 那么求值后的{{bar}} 将会绑定到scope中, 因为属性总是字符串,用这种写法你讲总会看到这个属性的值是字符串。
The '=' notation basically provides a mechanism for passing an object into your directive. It always pulls this from the parent scope of the directive, so this attribute will never have the {{}}
. So, if you have <my-directive foo="bar" />
it will bind whatever is in $scope.bar
into your directive in the foo
property of your directive's scope. Any change's you make to foo
within your scope will be refelected in bar
in the parent scope, and vice versa.
I haven't used the '&' notation nearly as much as the other too, so I don't know it as well as those two. From what I understand, it allows you to evaluate expressions from the context of the parent scope. So if you have something like <my-directive foo="doStuff()" />
, whenever you call scope.foo() within your directive, it will call the doStuff function in the directive's parent scope. I'm sure there's a lot more you can do with this, but I'm not as familiar with it all. Maybe someone else can explain this one in more detail.
If just the symbol is set in the scope, it will use the same name as the attribute to bind to the directives scope. For example:
scope: {
foo1: '@',
foo2: '=',
foo3: '&'
}
When including the directive, there would need to be the attributes foo1, foo2, and foo3. If you want a property in your scope different than the attribute name, you can specify that after the symbol. So, the example above would be
scope: {
foo1: '@bar1',
foo2: '=bar2',
foo3: '&bar3'
}
When including the directive, there would need to be the attributes bar1, bar2, and bar3, and these would get bound in the scope under properties foo1, foo2, and foo3 respectively.
I hope this helps. Feel free to ask questions with which I can clarify my answer.
Angular JS中自定义标签 属性绑定的解释的更多相关文章
- 关于在JS中设置标签属性
Attribute 该属性主要是用来在标签行内样式,添加.删除.获取属性.且适用于自定义属性. setAttribute("属性名",属性值“”):这个是用来设置标签属性的: re ...
- angular.js 中同步视图和模型数据双向绑定,$watch $digest $apply 机制
Angular.js 中的特性,双向绑定. 让视图的改变直接反应到数据中,数据的改变又实时的通知到视图,如何做到的? 这要归功于 scope 下面3个重要的方法: $watch $digest $ap ...
- jquery遍历标签中自定义的属性方法
在开发中我们有时会对html标签添加属性,如何遍历处理 <ul> <li name="li1" sortid="nav_1">aaaaa ...
- angular.js 中的作用域 数据模型 控制器
1.angular.js 作为后起之秀的前端mvc框架,他于传统的前端框架都不同,我们再也不需要在html中嵌入脚本来操作对象了.它抽象出了数据模型,控制器及视图. 成功解耦了应用逻辑,数据模型,视图 ...
- Angular.js中处理页面闪烁的方法详解
Angular.js中处理页面闪烁的方法详解 前言 大家在使用{{}}绑定数据的时候,页面加载会出现满屏尽是{{xxx}}的情况.数据还没响应,但页面已经渲染了.这是因为浏览器和angularjs渲染 ...
- 使用原生js创建自定义标签
使用原生js创建自定义标签 效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- 秒味课堂Angular js笔记------Angular js中的工具方法
Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...
- (网页)Angular.js 中 copy 赋值与 = 赋值 区别
转自st.gg Angular.js 中 copy 赋值与 = 赋值 区别 为什么用 $scope.user = $scope.master; $scope.master 会跟着 $scope.use ...
- JS中获取元素属性的逆天大法
给大家聊下js中获取元素属性的逆天大法,胆小慎入,切记切记!!! innerHTML.outerHTML.innerText .outerText.value.text().html(),val() ...
随机推荐
- Eclipse-Java代码规范和质量检查插件-SonarLint
SonarQube(Sonar)之前的提供的本地工具是需要依赖SonarQube服务器的,这样导致其运行速度缓慢. 新出的SonarLint的扫描引擎直接安装在本地,速度超快,实时探测代码技术债务,给 ...
- MongoDB小结16 - find【查询条件$in】
$in可以查询一个键的多个值 举例,每个人有爱好,假定为一个,数据太多,咱们用第二个参数来过滤一下 db.user.find({},{"_id":0}) { "hobby ...
- Python学习系列之内置函数
数学相关 abs(a):求取绝对值 max(list):求取list最大值 min(list):求取list最小值 sum(list):求取list元素的和 sorted(list):排序,返回排序后 ...
- Ubuntu 16.04 LTS 搭建LAMP
1. LAMP是一系列自由和开源软件的集合,包含了Linux.Web服务器(Apache).数据库服务器(MySQL)和PHP(脚本语言). Apache2 Web 服务器的安装 sudo apt i ...
- [React] Prevent Unnecessary Rerenders of Compound Components using React Context
Due to the way that React Context Providers work, our current implementation re-renders all our comp ...
- C#高级编程五十四天----Lookup类和有序字典
Lookup类 Dictionary<Tkey,TValue>仅仅为每一个键支持一个值.新类Lookup<Tkey,TValue>是.NET3.5中新增的,它类似与Dictio ...
- RELU 激活函数及其他相关的函数
RELU 激活函数及其他相关的函数 转载 2016年07月21日 20:51:17 45778 本博客仅为作者记录笔记之用,不免有很多细节不对之处. 还望各位看官能够见谅,欢迎批评指正. 更多相关博客 ...
- ubuntu14.04 的ibus不能卸载(安装fcitx输入法框架时可能有这个需求)。出现无system setting有用程序
每年的ubuntu新版本号公布,都会吸引一大批热血青年. 关注越多也让ubuntu越来越好了. 使用ubuntu的人都会在安装系统之后马上安装顺手的输入法,也可能不会.看人. 安装输入法,对于中文输入 ...
- 科普:google的数字图书馆
https://books.google.com/ngrams Google Ngram Viewer,她利用google所拥有的所有图书作为资源,为你提供单词和短语历年使用次数的展示图标.数据化了数 ...
- 使用MyBatis Generator自动生成MyBatis的代码
这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...