什么时候孤立作用域属性实际上被添加到范围有多大? [英] When do isolated scope properties actually get added to the scope?

查看:152
本文介绍了什么时候孤立作用域属性实际上被添加到范围有多大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

这篇关于什么时候孤立作用域属性实际上被添加到范围有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆