2015-07-20 92 views
9

有人可以请提供一个正确实施dom-if的例子吗?聚合物1.0:帮助使用dom-if

没有正确使用的例子是由official documentation提供的。 (抱歉,没有直接链接,必须使用左上方的菜单并选择dom-if)。

这是我到目前为止。显然,它不工作。

<template> 
    ... 
    <template is="dom-if" if="{{action}}=='Login'"> 
     <!-- Also tried: if="{{action=='Login'}}" --> 
    <a href="#">Forgot password?</a> 
    </template> 
    ... 
</template> 

回答

25

这是很麻烦的,但你必须这样做:

<template is="dom-if" if="[[_actionIsLogin(action)]]"> 
    <a href="#">Forgot password?</a> 
</template> 

<script> 
    Polymer({ 
    ... 
    _actionIsLogin: function(action) { 
     return action === 'Login'; 
    } 
    ... 
    }); 
</script> 

明确创建返回要么truefalse功能。

+0

这很有帮助,因为它让我思考了一种必要的方法。所以,upvoted!但不能接受,因为这个特定的代码不起作用。随意修改它!我会。谢谢! =] – Mowzer

+0

另外,我不认为你可以将一个参数传递给这样的函数?你可以吗? [看到这个问题](http://stackoverflow.com/questions/31441401/polymer-1-0-is-there-a-way-to-pass-an-argument-to-a-polymer-function-from-一个-AT)。 – Mowzer

+0

如果将它们声明为属性,则可以将参数传递给计算的绑定。在[文档](https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#annotated-computed)中,它表示“参数可以是相关属性或字符串或数字文字。” – Maria

4

我认为下面的例子是非常简单的,易于理解/实施(它不是在您所提供的链接):

https://www.polymer-project.org/1.0/docs/devguide/templates.html

从页面

.. 。

<div>{{user.name}}</div> 

<template is="dom-if" if="{{user.isAdmin}}"> 
    Only admins will see this. 
    <div>{{user.secretAdminStuff}}</div> 
</template> 
... 

希望它能帮助秒。

+0

它有帮助,因为您向我展示了其他文档。所以,upvoted!但它并没有回答这个问题,因为我正在试着评估一个'boolean'表达式,其中''user.isAdmin'只有一个简单的'boolean'值。表达评价是这里的诀窍,也是问题的重要部分。尚未接受任何答案,请随时再试! =] – Mowzer

1
<template> 
    <iron-ajax   
      auto 
      url="https://jsonplaceholder.typicode.com/todos" 
      handle-as="json" 
      last-response="{{baul}}"> 
    </iron-ajax> 

    <template is="dom-repeat" items="{{baul}}" > 
     <template is="dom-if" if="{{item.completed}}">{{item.title}} is completed<br></template> 
    </template> 


</template>