javascript Polymer 1.0:帮助使用 dom-if
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31511727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Polymer 1.0: Help using dom-if
提问by Let Me Tink About It
Can someone please provide an example of proper implementation of dom-if
?
有人可以提供一个正确实施的例子dom-if
吗?
No example of proper usage is provided by the official documentation. (Sorry there is no direct link. Must use menu in upper left and select dom-if
).
官方文档没有提供正确使用的示例。(抱歉没有直接链接。必须使用左上角的菜单并选择dom-if
)。
Here is what I have so far. Obviously, it is not working.
这是我到目前为止所拥有的。显然,它不起作用。
<template>
...
<template is="dom-if" if="{{action}}=='Login'">
<!-- Also tried: if="{{action=='Login'}}" -->
<a href="#">Forgot password?</a>
</template>
...
</template>
回答by agektmr
It's cumbersome, but you have to do this:
这很麻烦,但你必须这样做:
<template is="dom-if" if="[[_actionIsLogin(action)]]">
<a href="#">Forgot password?</a>
</template>
<script>
Polymer({
...
_actionIsLogin: function(action) {
return action === 'Login';
}
...
});
</script>
Explicitly create a function that returns either true
or false
.
显式创建一个返回true
或的函数false
。
回答by Zohar81
i think that the following example is pretty much straight forward and easy to understand/implement (it's not in the link you've provided):
我认为以下示例非常直接且易于理解/实现(它不在您提供的链接中):
https://www.polymer-project.org/1.0/docs/devguide/templates.html
https://www.polymer-project.org/1.0/docs/devguide/templates.html
from the page ...
从页面...
<div>{{user.name}}</div>
<template is="dom-if" if="{{user.isAdmin}}">
Only admins will see this.
<div>{{user.secretAdminStuff}}</div>
</template>
...
hope it helps.
希望能帮助到你。
回答by Cesar SC
<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>