Angular Directive 隔离范围上的可选双向绑定 [英] Optional two-way binding on isolate scope for Angular Directive

查看:24
本文介绍了Angular Directive 隔离范围上的可选双向绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解到您可以通过以下方式进行可选的反向"或回调绑定:

I just learned that you can have an optional 'reverse' or callback binding via:

scope: { parentScopeFunc: '&?' }

我想看看是否有办法对 2 向绑定做类似的事情.

I'm trying to see if there is a way to do something similar with the 2-way binding.

scope: { optional2WayBoundProp: '=?' }

我尝试使用链接函数的 element &attrs params 但随后我失去了对父级的 2 路绑定.该方法仅允许父对子更新.那么我也可能只是使用 @ 作用域机制.

I tried with the link function's element & attrs params but then I lose the 2-way-binding back to the parent. That method only allows for parent-to-child updates. Then I might as well just be using @ scope mechanism.

我发现了这个问题 Angular JS 指令,更改链接函数中的 2 路数据绑定,以便回答有关 =? 的主要问题.但是,它不能解决可选"非绑定值,例如 truefalse.

I found this question Angular JS directive, change a 2 way data binding in the link function so that answers the main question regarding =?. However it doesn't solve the 'optional' non-bound value such as true or false.

这是我想要完成的:

  • 编写一个嵌入内容的面板指令,除了标题区域外,它是可折叠的:
    <代码><我的面板><transcluded-header-content/><button ng-click="toggleCollapse()"/><transcluded-body-content ng-if="isExpanded"/></my-panel>

在某些情况下,我想在父作用域中缓存面板实例的折叠状态(因此视图的控制器可以确定如何缓存此信息的 2 向绑定):

in some cases I want to cache the collapsed state of the panel instance in a parent scope (hence the 2-way binding where the view's controller can determine how to cache this info):

<my-panel is-expanded="parentScopeProp">

我知道通过使用 = 赋值,像 undefined, true & 这样的表达式false 无法评估.

I understand that by using the = assignment, that expressions like undefined, true & false cannot be evaluated.

推荐答案

'=?' 是 ng 1.2.x 中的有效范围分配...

'=?' is a valid scope assignment as of ng 1.2.x...

...至于从可选的双向绑定中插入表达式的能力,这仍然有待争取.

...as for the ability to have a means of interpolating an expression from an optional two-way-binding, that is still up for grabs.

这篇关于Angular Directive 隔离范围上的可选双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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