什么时候孤立作用域属性实际上被添加到范围有多大?

什么时候孤立作用域属性实际上被添加到范围有多大?

问题描述:

我创建一些指令与一个孤立的范围和一些别名属性。例如:

I'm creating some directives with an isolated scope and some aliased properties. For example:

scope: {
   prop1: '@'
}

我的问题是,究竟何时对这些别名被添加到范围有多大?我跑与不被我的链接功能定义的属性的一些问题。看在控制台运行此的jsfiddle后: http://jsfiddle.net/rvd6x/

当我试图让正常没有定义的属性。如果我设法得到它后来通过一个函数(doStuff()),它的存在。或者,如果我做了 $超时 0它的存在。很显然,我可以使用 $超时解决方法我的问题,但我想知道为什么我不能只是指望已经拥有它的链接功能马上范围。它会是一种痛苦有注入 $超时在所有我的指令。

When I try to get the property as normal it is not defined. If I try to get it later through a function (doStuff()) it is there. Or if I do a $timeout with 0 it is there. Obviously I can workaround my issues by using the $timeout, but I want to know why I can't just expect the scope to already have it right away in the link function. It'd be kind of a pain to have to inject $timeout throughout all my directives.

我发现这工作指令定义中:

I found that this works inside the directive definition:

scope: {
    prop1: '@'
},
link: function(scope, element, attrs) {
    ...
    attrs.$observe('prop1', function(val) { 
        scope.prop1 = val || 'default'
    });
    ...
}

<div my-directive></div>

行为像

<div my-directive prop1="default"></div>