角指令 - 如果什么范围没有设置? [英] Angular directive - what if scope is not set?

查看:124
本文介绍了角指令 - 如果什么范围没有设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何时,范围在角指令定义设置scope属性被继承?

How are scope attributes being inherited when scope is not set in Angular directive definition?

角文档(在'指令定义对象)有两种情况:当范围真正或者范围是一个对象( {} )。如果范围不设置或为假。如何在这种情况下,该作用域继承的属性和它的孩子们?

In Angular docs (under 'Directive Definition Object') there are two cases: when scope is true or when scope is an object ({}). What if scope is not set or is false. How are attributes inherited in this case for the scope and for it's children?

推荐答案

在范围设置为false(也是默认值),范围行为因您是否做 transclusion 或没有。

When scope is set to false(also default value) in directive definition, behavior of scope differs based on whether you are doing transclusion or not.

如果你不 transclude ,它pretty多采用父范围。就像如果你没有使用的指令,并直接在页面上写的链接模板。

If you do not transclude, it pretty much uses parent scope. Just as if you did not used a directive and written the linked template directly in the page.

小提琴

Transclusion总是创建一个子范围,但它必将以父范围在陌生的路上时,该指令的范围是假的。你可以读出父范围,但只要你写到它的未绑定到父作用域的同一属性的属性了,但现在的孩子范围财产,除非该属性是一个对象。

Transclusion always creates a child scope but it's bound to parent scope in a strange way when the directive scope is false. You can "read" parent scope but as soon as you "write" to a property it's not bound to parent scope's same property anymore but now a property on child scope unless that property is an object.

Transcluded小提琴

Child scope is the ones in green border.

第一次尝试更改父范围。输入在VAR输入框的东西,你会看到孩子示波器的变种也会发生变化。但是,当你在子作用域改变VAR它不更改父范围,改变母范围VAR不影响孩子范围,因为当你从小孩范围写信给VAR债券被打破。这并不适用于对象(试穿sp.var一样,你会看到,你不能打破债券)。据开发商介绍,这是预期的和/或预期行为。

Try changing the parent scope first. Enter something in the "var" input field, you will see that the child scope's var will also change. But when you change var in child scope it's not changing the parent scope, and changing the var in parent scope does not affect child scope as the bond is broken when you wrote to var from child scope. This does not apply to the objects (try the same on sp.var, you will see that you cannot break the "bond"). According to developers this is the expected and/or intended behavior.

这篇关于角指令 - 如果什么范围没有设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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