看到自定义标签的文档时,文档作者解释的能力实在太弱,也可能是本人太笨,一下绕不过来。 看了一个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中自定义标签 属性绑定的解释的更多相关文章

  1. 关于在JS中设置标签属性

    Attribute 该属性主要是用来在标签行内样式,添加.删除.获取属性.且适用于自定义属性. setAttribute("属性名",属性值“”):这个是用来设置标签属性的: re ...

  2. angular.js 中同步视图和模型数据双向绑定,$watch $digest $apply 机制

    Angular.js 中的特性,双向绑定. 让视图的改变直接反应到数据中,数据的改变又实时的通知到视图,如何做到的? 这要归功于 scope 下面3个重要的方法: $watch $digest $ap ...

  3. jquery遍历标签中自定义的属性方法

    在开发中我们有时会对html标签添加属性,如何遍历处理 <ul> <li name="li1" sortid="nav_1">aaaaa ...

  4. angular.js 中的作用域 数据模型 控制器

    1.angular.js 作为后起之秀的前端mvc框架,他于传统的前端框架都不同,我们再也不需要在html中嵌入脚本来操作对象了.它抽象出了数据模型,控制器及视图. 成功解耦了应用逻辑,数据模型,视图 ...

  5. Angular.js中处理页面闪烁的方法详解

    Angular.js中处理页面闪烁的方法详解 前言 大家在使用{{}}绑定数据的时候,页面加载会出现满屏尽是{{xxx}}的情况.数据还没响应,但页面已经渲染了.这是因为浏览器和angularjs渲染 ...

  6. 使用原生js创建自定义标签

    使用原生js创建自定义标签 效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  7. 秒味课堂Angular js笔记------Angular js中的工具方法

    Angular js中的工具方法 angular.isArray angular.isDate angular.isDefined angular.isUndefined angular.isFunc ...

  8. (网页)Angular.js 中 copy 赋值与 = 赋值 区别

    转自st.gg Angular.js 中 copy 赋值与 = 赋值 区别 为什么用 $scope.user = $scope.master; $scope.master 会跟着 $scope.use ...

  9. JS中获取元素属性的逆天大法

    给大家聊下js中获取元素属性的逆天大法,胆小慎入,切记切记!!! innerHTML.outerHTML.innerText .outerText.value.text().html(),val() ...

随机推荐

  1. 我的arcgis培训照片9

    来自:http://www.cioiot.com/successview-547-1.html

  2. Linux学习系列之MySQL备份

    MySQL排除表备份 #!/bin/bash #created by 90root #date: 20160809 date_y=$(date +%Y) date_m=$(date +%m) time ...

  3. ArcGIS 教程:Workflow Manager 高速浏览

    应用程序概述 Workflow Manager 用户界面提供了用于在整个作业的生命周期中创建和管理作业的工具. 下面全部信息将会在本帮助文档的兴许章节中进行具体的说明. 文件菜单 新建 - 在系统中创 ...

  4. Cocos2d-x旧引擎文件夹结构

    转自:http://blog.csdn.net/lwuit/article/details/7870395 Cocos2d-x的文件夹结构例如以下: 文件夹的详细结构介绍例如以下: Box2D:物理引 ...

  5. apache ab 測试 apr_socket_connect(): 因为目标机器积极拒绝 无法连接

    遇到这样的情况通常是你开的并行数量太多了... 比如:ab -c 1000 -n 10000 http://localhost/index.html 如此大的请求就会挂掉,只是还是有补救措施的,能够通 ...

  6. S5P4418裸机开发系列教程--源代码下载

    S5P4418裸机系列教程之stdio S5P4418裸机系列教程之shell命令行 S5P4418裸机系列教程之串口回显 S5P4418裸机系列教程之复位測试 S5P4418裸机系列教程之led跑马 ...

  7. 量化分析师的Python日记【第1天:谁来给我讲讲Python?】

    量化分析师的Python日记[第1天:谁来给我讲讲Python?]薛昆Kelvin优矿 001 号员工2015-01-28 15:48 58 144克隆 ###“谁来给我讲讲Python?” 作为无基 ...

  8. android XXXActivity和getApplicationContext()差别

    从接触android起,到处都能看到context(上下文)的身影,查看源代码之后你会发现,它仅仅是个抽象类,详细实现都在ContextWrapper实现. 当你去查看android的源代码时,你会发 ...

  9. [Java] 监控java对象回收的原理与实现

    监控Java对象回收的原理与实现 一.监控Java对象回收的目的 监控Java对象是否回收的目的是:为了实现内存泄露报警. 内存泄露是指程序中对象生命周期(点击查看详情)已经进入不可见阶段,但因为编码 ...

  10. [办公应用]如何制作二Y轴图(excel)

    有时候我们会遇到一种图表,就是X轴一致,可是Y轴的数据相差很大.如下图中,年龄和收入就不是一个数量级,在图表中显示的时候,“年龄”的曲线根本看不到(表中数据仅供举例): 解决的方法就是使用双Y轴显示, ...