2015-07-20 88 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>