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() ...
随机推荐
- mac idea快捷键(部分常用)
shift+F6重命名 shift+enter 换到下一行 shift+F8等同eclipse的f8跳到下一个断点,也等同eclipse的F7跳出函数 F8等同eclipse的f6跳到下一步F7等同e ...
- Eclipse编辑YAML插件-YEdit
官网:https://github.com/oyse/yedit 离线版本:(链接: https://pan.baidu.com/s/1b1j2gQ 密码: wyyb) 安装方法:直接复制JAR包到P ...
- webpack 输出多个文件
http://react-china.org/t/webpack/1870/2 webpack 文章 entry = { "button": "demo/button/i ...
- win7如何更改语言教程
一.首先从桌面左下角的开始菜单中找到“控制面板”,然后打开,如下图所示: 打开电脑控制面板 二.进入控制面板之后,我们再进入“时钟.语言和区域”设置,如下图所示: 电脑语言改成英文方法 三.进入电脑语 ...
- iOS自己定义返回button(不影响返回手势)
此方法能够自己定义返回button,且不影响返回手势. 新方法: self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] in ...
- HDU 1796 How many integers can you find(容斥原理+二进制/DFS)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- 【Android】自己定义View、画家(画布)Canvas与画笔Paint的应用——绘图、涂鸦板app的实现
利用一个简单的绘图app来说明安卓的图形处理类与自己定义View的应用. 例如以下图,有一个供用户自己随意绘图.涂鸦的app. 这里不做那么花俏了,仅提供黑白两色.但能够改变笔尖的粗细. 实质上这里的 ...
- Linux进程间通信 共享内存+信号量+简单样例
每个进程都有着自己独立的地址空间,比方程序之前申请了一块内存.当调用fork函数之后.父进程和子进程所使用的是不同的内存. 因此进程间的通信,不像线程间通信那么简单.可是共享内存编程接口能够让一个进程 ...
- 同一台机子 打开两个tomcat
由于项目測试须要.须要在机子上单独搭建一个图片server,所以就要同一时候打开两个tomcat,这里记录方法. 1. 首先复制一个新的tomcat程序: 2.在新的tomcat程序中改动server ...
- 【JSOI 2008】 最小生成树计数
[题目链接] 点击打开链接 [算法] 笔者做这题参考了这篇博客 : https://blog.sengxian.com/solutions/bzoj-1016 推荐阅读 首先,我们需要知道三个定理 : ...