简体   繁体   English

JSrender:如果条件不能访问内部的父变量

[英]JSrender: cannot access parent variable inside if condition

as the title says, the problem is clear: 如标题所示,问题很明显:

within the construction: 在构造内:

{{if is_completed == 1}}
 <div>
   <p>{{:#parent.parent.data.myproperty}}</p>
 </div>
{{/if}}

the parent properties are not visible! 父属性不可见!

I solved it by creating construction like:{{for movies ~myproperty=myproperty}} in parent loop, and ~myproperty is visible inside IF conditions, but what if I have several variables, what if I have many-level nesting data structure? 我通过在父循环中创建类似{{的电影〜myproperty = myproperty}}的构造来解决它,并且〜myproperty在IF条件内可见,但是如果我有多个变量怎么办,如果我有多层嵌套数据结构怎么办?

The {{if ...}} block adds another view, so means you need to add a .parent to step up through that view, as in: {{:#parent.parent.parent.data.myproperty}} {{if ...}}块添加了另一个视图,因此意味着您需要添加.parent才能逐步浏览该视图,例如: {{:#parent.parent.parent.data.myproperty}}

You can pass in variables as you said ( ~myproperty=... ) and they will be visible to any depth of nesting. 您可以按照您所说的( ~myproperty=... )传入变量,它们在任何嵌套深度都将可见。

Your variable can be an object too such as the current data object: ~myObj=#data : 您的变量也可以是一个对象,例如当前数据对象: ~myObj=#data

{{sometag a=b ~myObj=#data}}
    ....
    {{:~myObj.myproperty}}
    ...
{{/sometag}}

so you don't need a separate variable for each property. 因此您不需要为每个属性单独设置变量。

You can also access the top-level data object and drill down from there: 您还可以访问顶级数据对象并从那里向下钻取:

{{:~root.foo...myproperty}} . {{:~root.foo...myproperty}}

And finally you can use #get("item") to step up through any number of {{if}} blocks and get the nearest "item" view (ie the item view for a repeating {{for ...}} block). 最后,您可以使用#get("item")逐步遍历任意数量的{{if}}块,并获取最近的“ item”视图(即重复{{for ...}}块的项目视图)。

So you would write: 所以你会写:

{{:#get("item").data.myproperty}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM