访问模板块内的组件范围 [英] Accessing component scope within template block

查看:77
本文介绍了访问模板块内的组件范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个名为 my-scope 的组件,仅定义为:

if I have a component called my-scope which is defined only as:

export default Ember.Component.extend({
    foo: 'bar'
})

然后我使用模板中的组件,如下所示:

and then I use the component in a template, like so:

{{#my-scope}}
    {{foo}}
{{/my-scope}}

如何达到周边块组件的范围?我试过:

How can I reach the scope of the surrounding block component? I have tried:


  • {{foo}}

  • {{component.foo}}

  • {{view.foo}}

  • {{foo}}
  • {{component.foo}}
  • {{view.foo}}

也许这是我需要使用视图的情况?希望不是,但也许是这样的情况。

Maybe this is a case where I need to use a view? Hoping not but maybe that is the case.


注意:我的用例更复杂,但我们只是说正常的绑定方法组件 {{#my-scope foo = outsideFoo}} ... {{/ my-scope}} 不是一个可行的例子


推荐答案

您可以使用 _view.parentView 访问<$组件内的c $ c> foo 属性。 (因为在 _view 中有一个下划线,对我来说,这意味着它不是公共API,所以他们可能会在将来摆脱它 - 但它现在在... :)

You can use _view.parentView to access the value of foo property inside the component. (Because there is an underscore in _view, to me that means that it's not public API, so they might get rid of it in the future - but it's around for now :))

如下所示:

{{#x-test}} Inside c1: {{ _view.parentView.foo.name }} {{/x-test}}

工作演示 here

这篇关于访问模板块内的组件范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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