ng-directive & scope & compile & link
ng-if
- if false template will not render to dom
- create a scope for itself
- evaluation expression based on the scope
ng-show/hide
- it won’t create scope for this directive
- evaluation expression based on parent’s scope
ng-repeat
- creates a scope for each instantiated template(item)
directive scope
scope spec
The scope property can be false, true, or an object:
false
(default): No scope will be created for the directive. The directive will use its parent’s scope.true
: A new child scope that prototypically inherits from its parent will be created for the directive’s element.
If multiple directives on the same element request a new scope, only one new scope is created.{...}
(an object hash): A new “isolate” scope is created for the directive’s element. (still can access $parent, there’ll nothing on__proto__
)
The ‘isolate’ scope differs from normal scope in that it does not prototypically inherit from its parent scope.
This is useful when creating reusable components, which should not accidentally read or modify data in the parent scope.
directive scope bind(scope parameter)
获取的值是设置在指令属性上的,而非模板上的.
reftoparent
是directive scope的,item
是parent scope的
12<g-list tag='list-directive' reftoparent="items[0]">dattass111111</g-list> // heretemplate: '<div ><span>aaaaa</span></div>' // not here
@
or@attr
: 获取属性的值, 从attr上也可获取 The result is always a string, bind object use<
gesus- string value to be passed to directive scope
- on way bind
- can be an interpolated string(ng-expression) eg.
=
or=?attr
or=*?attr
: 和parent scope的值双向绑定- object to be passed to directive scope eg.
b
is unacceptable
- two way bind(between parent and directive scope)
- object to be passed to directive scope eg.
<
or<?attr
: 单向绑定, 依据$watch来判断, 如果绑定的是对象会出现双向绑定的结果
- object to be passed to directive scope
- one way bind(between parent and directive scope)
&
or&attr
: 绑定parent scope context的express 引用, usually function reference.- function to be accessible from directive scope
- and directive can execute it
directive scope mix result
no scope + no scope
=> Two directives which don’t require their own scope will use their parent’s scopechild scope + no scope
=> Both directives will share one single child scopechild scope + child scope
=> Both directives will share one single child scopeisolated scope + no scope
=> The isolated directive will use it’s own created isolated scope. The other directive will use its parent’s scopeisolated scope + child scope
=> Won’t work! Only one scope can be related to one element. Therefore these directives cannot be applied to the same element.isolated scope + isolated scope
=> Won’t work! Only one scope can be related to one element. Therefore these directives cannot be applied to the same element.
demo code for scope parameter
|
|
demo code FOR directives link&compile and scope
demo.html
|
|
list.js
|
|